time

Stata programs to convert time strings into fractions of a day and back again (for Stata 9 and below)

By Denver Kirrane and Tony Brady

Note. Since version 10, Stata has the ability to natively handle times. We therefore only recommend you use the commands below if you are using version 9 or below of Stata.

How do you handle time of day information such as 3:15am or 7:30pm in Stata? One solution, first suggested by Bill Gould is to convert times into fractions of a day like this:

Time Fraction of day elapsed
3:00am 0.125
6:00am 0.25
12:00 (midday) 0.5
8:30pm 0.85417
00:00 (midnight) 0.0

So that in the same way that dates are stored as whole days since 1 Jan 1960, times are stored as fractions of a day since midnight (midnight being the start of a new day). Storing times in this elapsed time format has the nice property that elapsed times can be added to elapsed dates to give a finer graded elapsed date format. Elapsed date-times can be compared to one another to see if one occurred before or after another using the usual < or > operators.

Example

18th October 2003 is 15996 in Stata's elapsed date format. So 15996.25 is 6:00am on 18th October 2003.

6:00pm on the same day would be 15996.75 and since 15996.75 > 15996.25 then Stata can tell that 6pm on the 18th October 2003 is later than 6:00am on 18th October 2003.

Unfortunately Stata doesn't recognise this finer date format so you can't use the format command to show the time of day stored in a value like 15996.25.

However, we've provided you with a few tools to make converting times to elapsed times and back again easy. These are str2time and time2str.

str2time

str2time converts a string variable containing times in 24 hour clock format (HH:MM or HH:MM:SS) into an elapsed time format (a numeric value between 0 and 1).

Here's a dataset after running the command:

str2time tod, generate(etod)
tod (string) etod (double)
03:17 .13680556
15:26 .64305556
13:23 .55763889
14:30 .60416667
16:25 .68402778
02:36 .10833333
14:50 .61805556
01:27 .06041667
13:19 .55486111
20:54 .87083333

time2str

time2str does the opposite to str2time. It converts a numeric variable containing elapsed times to a string variable containing times in 24 hour clock format (HH:MM or HH:MM:SS).

Here's a dataset after running the command:

time2str etod, generate(tod)
etod (double) tod (string)
.25486111 06:06
.04444444 01:03
.42361111 10:09
.89791667 21:33
.52152778 12:31
.84097222 20:11
.21041667 05:02
.56388889 13:31
.26458333 06:20
.94722222 22:44

Technical note

To convert a string of format HH:MM:SS to an elapsed time the formula is:

elapsed time = (HH + MM/60 + SS/3600)/24

To retrieve the hours, minutes and seconds from an elapsed time, e, we use:

HH = int(e*24)
MM = int(60*(e*24 - HH))
SS = int(60*(60*(e*24 - HH) - MM))

Because computers use binary to store numbers they cannot always store fractions precisely. We have to do some rounding in Stata to prevent anomalies occurring when translating between the two formats.

Installation

To obtain str2time and time2str type the following into Stata:

net from https://www.sealedenvelope.com/

and follow the instructions on screen. This will ensure the files are installed in the right place and you can easily uninstall the command later if you wish.