-2

I'm calling an api and getting the datetime field in this format /Date(1500462505221)/ . My motive is to convert it to this format "2018-05-11 23:25:47" . How do I accomplish this ?

Nilesh Kumar Guria
  • 149
  • 2
  • 2
  • 8

3 Answers3

6

You can use datetime library to make life simple:

from datetime import datetime

datetime.fromtimestamp(1485714600).strftime("%Y-%m-%d %I:%M:%S")
nick
  • 1,090
  • 1
  • 11
  • 24
kanishk
  • 91
  • 9
1

So here is what i got to convert time in human readable format, maybe this can help:

import time
time.strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime(1500462505221))

or

time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1500462505221))

and one more thing i tried this example by removing last three digit of as follows and got this as output:

>>time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1500462505))
>>'2017-07-19 11:08:25'

You can also refer this link: [https://www.systutorials.com/241698/how-to-convert-epoch-timestamp-to-human-readable-date-format-in-python/

Deepak Verma
  • 617
  • 5
  • 18
1

You can do something like this using time and re modules:

import re, time

epoch_str = "/Date(1500462505221)/"
date = re.match(r'/Date\((\d+)\)/', epoch_str).groups()[0]
epoch_fmt = time.strftime('%Y-%M-%d %H:%M:%S', time.gmtime(float(date)))
# Or you can do
# date_ = time.gmtime(float(date))
# epoch_fmt = '{}-{}-{} {}:{}:{}'.format(date_.tm_year, date_.tm_mon, date_.tm_mday, date_.tm_hour, date_.tm_min, date_.tm_sec) 
print(epoch_fmt)
# 49517-20-07 04:20:21
Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43