-1

I have "Fri Dec 04 07:00:16 CST 2020" timestamp, need to convert it to Unix/Epoch time in python code And how to convert reverse [Epoch time to above format]

Thanks,

user3319565
  • 161
  • 1
  • 1
  • 7

2 Answers2

1

You can use dateparser dateparser . Here is the code

>>> import dateparser
>>> dateparser.parse('Fri Dec 04 07:00:16 CST 2020').strftime('%s')
'1607045416'

you have to install dateparser first
For reverse format you can do the same by passing argument to strftime.strftime

  • Thanks, I hit with below issue, Traceback (most recent call last): File "C:/Users/arvinaya/PycharmProjects/study_assignments/TrainingTasks/dictTest.py", line 27, in print(dateparser.parse('Fri Dec 04 07:00:16 CST 2020').strftime('%s')) ValueError: Invalid format string – user3319565 Dec 04 '20 at 04:14
0

You can use time and datetime module:

>>> import time
>>> import datetime
>>> now = time.time()
>>> now
1607055249.4696143
>>> datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S')
'2020-12-04 01:14:09'

If you want to modify the format, you can take a look to the strftime documentation here.