Converting string to datetime results in incorrect hours/minutes/seconds
Asked Answered
M

1

7

Objective

I am converting a vector of strings of the following format "MM/DD/YYYY HH:MM:SS" to a Stata recognizable datetime using the clock() function.

The problem

The resulting converted datetime is not equivalent to the input. Here is the code that I've used,

gen datetime = clock(dt, "MDY hms")
format datetime %tc

and here is the resulting data, where the original string is dt and is a str19 type in %19s format and datetime is the converted variable as a float formatted as %tc,

dt datetime
1/1/2016 1:00:00 01jan2016 01:00:27
1/1/2016 2:00:00 01jan2016 01:59:25
1/1/2016 3:00:00 01jan2016 03:00:35
1/1/2016 4:00:00 01jan2016 03:59:34

What I've done

I have read through the the Stata User Manual's Chapter 24 Working with dates and times as well as Datetime translation and could not parse out any clues to what is happening, so any help is greatly appreciated.

Sample Data

clear
input str19 dt
"1/1/2016 1:00:00" 
"1/1/2016 2:00:00" 
"1/1/2016 3:00:00" 
"1/1/2016 4:00:00" 
"1/1/2016 5:00:00" 
"1/1/2016 6:00:00" 
"1/1/2016 7:00:00" 
"1/1/2016 8:00:00" 
"1/1/2016 9:00:00" 
"1/1/2016 10:00:00"
end
Musaceous answered 17/10, 2021 at 17:16 Comment(3)
You need to specify -double- as variable type. This is mentioned repeatedly in the documentation.Sever
Beautiful, sorry I missed that. Thanks @NickCoxMusaceous
Credit for clearly stated problem.Sever
S
1

You need to specify double as variable type. This is mentioned repeatedly in the documentation.

Sever answered 21/6, 2023 at 0:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.