Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

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

From   "Christian Holz (Stata list)" <>
Subject   Re: st: Extract date and time from a date/time variable
Date   Wed, 11 May 2011 09:59:13 -0400

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.


On Wed, May 11, 2011 at 9:46 AM, Pinaki Mitra <> wrote:
> Hello,
>    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"?
> Thank you,
> Pinaki Mitra
> *
> *   For searches and help try:
> *
> *
> *

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index