2

I want to convert a time(String) to unix timestamp, then convert it back to readable datetime, however, there are 7 hours different. What is the problem in my python code?

import datetime

#Convert a Datetime String to unix timestamp   
def get_Timestamp_from_String(datetimeStr):    

    timestamp = int(datetime.datetime.strptime(datetimeStr, '%Y-%m-%d %H:%M:%S').timestamp())*1000

    return timestamp


# Convert unix timestamp to DateTime    
def get_Datetime_from_Timestamp(timestamp):

    dt = pd.to_datetime(timestamp, unit='ms')

    return dt

Test the datetime: "2016-04-01 01:10:04", however, if I convert it back, then I got the result: '2016-04-01 08:10:04', there are 7 hours different

timestamp = get_Timestamp_from_String("2016-04-01 01:10:04")
res = get_Datetime_from_Timestamp(timestamp)
print(res)

>>>Timestamp('2016-04-01 08:10:04')

What is the problem in my code?

2
  • Timezone issues? Commented Sep 13, 2017 at 11:56
  • @IanS I feel very strange, I didn't give any timezone in both functions, so the default timezone should be applied. Commented Sep 13, 2017 at 12:12

1 Answer 1

3

I think you need to_datetime twice, for ms divide by 10**6:

datetimeStr = '2016-04-01 01:10:04'
timestamp = pd.to_datetime(datetimeStr).value //10**6
print (timestamp)
1459473004000

dt = pd.to_datetime(timestamp, unit='ms')
print (dt)
2016-04-01 01:10:04

For ns, native format of timestamps in numpy dividing is not necessary:

datetimeStr = '2016-04-01 01:10:04'
timestamp = pd.to_datetime(datetimeStr).value
print (timestamp)
1459473004000000000

dt = pd.to_datetime(timestamp, unit='ns')
print (dt)
2016-04-01 01:10:04

But if have timezone information:

datetimeStr = '2016-04-01 01:10:04-07:00'
timestamp = pd.to_datetime(datetimeStr).value //10**6
print (timestamp)
1459498204000

dt = pd.to_datetime(timestamp, unit='ms')
print (dt)
2016-04-01 08:10:04
Sign up to request clarification or add additional context in comments.

2 Comments

@jezrael , thanks very much for the answer. But, I think, it is so strange, that the result from pd.to_datetime(...) is different from datetime.datetime.strptime(...), I can not understand it...
Hard question for me - but I think pandas to_datetime is more general.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.