Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to parse string to timestamp-with-timezone in python

I try to parse string to time-stamp with timezone format.

here is an example

"2016-02-18 16:13:07+09"

i want to know parsing this string format to time-stamp format in python.

how can i do that?

like image 826
Kyehee Kim Avatar asked Oct 17 '25 23:10

Kyehee Kim


1 Answers

Is the UTC offset format in your string +09 or +0900 ?

If the offset in your string is 0900 you can use the below .If your UTC offset is only +09 as you mentioned in your question , you can pad the string with 00 and get the below code to work .

Code:

import datetime  
time="2016-02-18 16:13:07+0900"  
new_time=datetime.datetime.strptime(time,"%Y-%m-%d %H:%M:%S%z")  
print(new_time)  
new_time_python=datetime.datetime.strftime(new_time,"%m-%d-%y")  
print(new_time_python)  

Output

2016-02-18 16:13:07+09:00  
02-18-16 
like image 107
Rahul.M Avatar answered Oct 19 '25 13:10

Rahul.M