Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Extract date and time from a date/time variable

From   Nick Cox <>
Subject   Re: st: Extract date and time from a date/time variable
Date   Wed, 11 May 2011 18:08:48 +0100

There is various minor confusion here, although some can be put down
to careless wording.

Thus it is wrong, strictly (pedantically, if you prefer, but that's
not a pejorative in my vocabulary), to say that a value of a date-time
variable is

"01mar2011 11:56:00"

That is, rather, a value represented using a display format %tc. The
_value_ is a number, in this case more than a trillion,

. di %15.0f clock("1mar2011 11:56:00", "DMYhms")

That formatted %tc is, as said,

. di %tc clock("1mar2011 11:56:00", "DMYhms")
01mar2011 11:56:00

Any way, always recalling that date-times are just numbers underneath
a format underlines that the time of day alone is just that date-time
MINUS the date-time the previous midnight. For _that_, the %tc format
is not appropriate as it shows a time on 1 January 1960, so you need a
different format, e.g.

. di %tc_hh:mm:SS clock("1mar2011 11:56:00", "DMYhms") -
clock("1mar2011 00:00:0 0", "DMYhms")

In general, the previous midnight will be cofd(dofc(date_time)).

You could go the long way around, as Christian suggests, extracting
hours, minutes and seconds, and reconstructing the time that way, but
I can't think of a good reason to do that. Nor is it correct to
suppose that you might need some different way of representing times


On Wed, May 11, 2011 at 2:59 PM, Christian Holz (Stata list)
<> wrote:

> Stata help under "whelp dates and times" reads as follows:
> Let t be a %tc variable.  The following functions will extract components of t:
>            ----------------------------------------------------------------
>                                        Result if t = tc(05jul1972-21:38:02)
>            Function         Returns         (i.e., t = 394,839,482,000)
>            ----------------------------------------------------------------
>            hh(t)            time of day, hours       21
>            mm(t)            time of day, minutes     38
>            ss(t)            time of day, seconds     2.000
>            ----------------------------------------------------------------
> Thus, you can extract the hours, minutes and seconds into new variables as
> generate hrs=hh(ActivityDate)
> generate mins=mm(ActivityDate)
> generate secs=ss(ActivityDate)
> I don't think the way Stata deals with date and time allows you to
> store times (i.e. as sort of a "time" data type) without a date, but
> you could generate (e.g. for display purposes) a string variable from
> these hrs, mins, secs variables or calculate a "seconds since
> midnight" variable.

Pinaki Mitra

>>    I have a date/time variable "ActivityDate" which is double and in %tc format. For example, a value of "ActivityDate" is "01mar2011 11:56:00". I need to extract date and time into two variables. I did gen ActivityDay=dofc( ActivityDate) and format ActivityDay %dN/D/Y which provided me the date as 03/01/11. What can I do to extract "11:56:00" into a new variable "ActivityTime"?

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index