1

I have a JSON object with a date that returns

print row['ApplicationReceivedDateTime'] /Date(1454475600000)/ 

how do I process this using the pythons datetime module?

print type(row['ApplicationReceivedDateTime']) 

returns <type 'unicode'>

print repr(row['ApplicationReceivedDateTime'])

returns u'/Date(1454475600000)/'

5
  • 2
    What would be the output, is that as string? Commented Oct 17, 2019 at 15:29
  • What format is the date number in? Commented Oct 17, 2019 at 15:31
  • Where are the slashes coming from? Show us the output of print repr(row['ApplicationReceivedDateTime']). Commented Oct 17, 2019 at 15:32
  • @JohnGordon that is what prints out Commented Oct 17, 2019 at 15:33
  • first answer pretty much explains it stackoverflow.com/questions/54092831/… Commented Oct 17, 2019 at 15:37

3 Answers 3

2

That looks like milliseconds. Try dividing by 1000.

import datetime as dt >>> dt.datetime.fromtimestamp(1454475600000 / 1000) datetime.datetime(2016, 2, 2, 21, 0) 

If the date is in the string format per your question, extract the numeric portion using re.

date = '/Date(1454475600000)/' >>> dt.datetime.fromtimestamp(int(re.findall(r"\d+", date)[0]) / 1000) datetime.datetime(2016, 2, 2, 21, 0) 
Sign up to request clarification or add additional context in comments.

1 Comment

I was going to post it using: epoch = int(re.sub("[^\d\.]", "", a))/1000
0

You probably want

datetime.datetime.strptime(string_date, "%Y-%m-%d %H:%M:%S.%f") 

And the values of Year, Month, Day, Hour, Minute, Second and F, for that you can write a manual function for that like this

def generate_date_time_str(date_str): """Login to parse the date str""" return date_str 

the date_str will look link this

"%Y-%m-%d %H:%M:%S.%f"

There is no python module directly convert any random date str to DateTime object

Comments

0

You can use re to get the integer value and then use datetime.datetime.fromtimestamp to get the date value:

from datetime import datetime import re string_time = row['ApplicationReceivedDateTime'] parsed_time = int(re.search('\((\d+)\)', string_time)[1]) / 1e3 #1e3 == 1000 rcvd_date = datetime.fromtimestamp(parsed_time) print(rcvd_date.strftime('%Y-%m-%d %H:%M:%S')) 

Prints:

'2016-02-03 05:00:00' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.