Title
[D] datetime translation -- String to numeric date translation functions
Syntax
The string-to-numeric date and time translation functions are
Desired SIF type | String-to-numeric translation function
------------------+---------------------------------------
datetime/c | clock(HRFstr, mask [, topyear])
datetime/C | Clock(HRFstr, mask [, topyear])
|
date | date(HRFstr, mask [, topyear])
|
weekly date | weekly(HRFstr, mask [, topyear])
monthly date | monthly(HRFstr, mask [, topyear])
quarterly date | quarterly(HRFstr, mask [, topyear])
half-yearly date | halfyearly(HRFstr, mask [, topyear])
yearly date | yearly(HRFstr, mask [, topyear])
----------------------------------------------------------
where
HRFstr is the string value (HRF) to be translated,
topyear is described in Working with two-digit years, below,
and mask specifies the order of the date and time components and is a string composed of a sequence of these elements:
Code | Meaning
------+---------------------------------------
M | month
D | day within month
Y | 4-digit year
19Y | 2-digit year to be interpreted as 19xx
20Y | 2-digit year to be interpreted as 20xx
|
h | hour of day
m | minutes within hour
s | seconds within minute
|
# | ignore one element
----------------------------------------------
Blanks are also allowed in mask, which can make the mask easier to read, but they otherwise have no significance.
Examples of masks include
"MDY" HRFstr contains month, day, and year, in that order.
"MD19Y" means the same as "MDY" except that HRFstr may contain two-digit years, and when it does, they are to be treated as if they are
4-digit years beginning with 19.
"MDYhms" HRFstr contains month, day, year, hour, minute, and second, in that order.
"MDY hms" means the same as "MDYhms"; the blank has no meaning.
"MDY#hms" means that one element between the year and the hour is to be ignored. For example, HRFstr contains values like "1-1-2010 at
15:23:17" or values like "1-1-2010 at 3:23:17 PM".
Description
These functions translate dates and times recorded as strings containing human readable form (HRF) to the desired Stata internal form (SIF). See [D]
datetime for an introduction to Stata's date and time features.
Also see Using dates and times from other software in [D] datetime.
Remarks
Remarks are presented under the following headings:
Introduction
Specifying the mask
How the HRF-to-SIF functions interpret the mask
Working with two-digit years
Working with incomplete dates and times
Translating run-together dates, such as 20060125
Valid times
The clock() and Clock() functions
Why there are two SIF datetime encodings
Advice on using datetime/c and datetime/C
Determining when leap seconds occurred
The date() function
The other translation functions
Introduction
The HRF-to-SIF translation functions are used to translate string HRF dates, such as "08/12/06", "12-8-2006", "12 Aug 06", "12aug2006 14:23", and "12
aug06 2:23 pm", to SIF. The HRF-to-SIF translation functions are typically used after importing or reading data. You read the date information into
string variables and then the HRF-to-SIF functions translate the string into something Stata can use, namely, an SIF numeric variable.
You use generate to create the SIF variables. The translation functions are used in the expressions, such as
. generate double time_admitted = clock(time_admitted_str, "DMYhms")
. format time_admitted %tc
. generate date_hired = date(date_hired_str, "MDY")
. format date_hired %td
|