python json date object to python datetime - python

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)/'

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)

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

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'

Related

What is the quickest way to increment date string YYYY-MM-DD in Python?

In Pandas, I am using dates with string format YYYY-MM-DD
What is the quickest way to increment the date with the result in YYYY-MM-DD format?
d1 = '2018-02-10'
I want to increment it by 1 and get the result back as a string:
d1_inc = '2018-02-11'
Pure Python
You can use the datetime module, part of the standard library. There are 3 steps:
Convert string to datetime object via strptime.
Add a day via timedelta.
Convert resulting datetime object back to string via strftime.
Here's a demo:
from datetime import datetime, timedelta
x = '2017-05-15'
res = (datetime.strptime(x, '%Y-%m-%d') + timedelta(days=1)).strftime('%Y-%m-%d')
print(res)
# 2017-05-16
Pandas
The equivalent steps can be performed using 3rd party Pandas:
x = '2017-05-15'
# choose some combination of below methods
res = (pd.Timestamp(x) + pd.DateOffset(days=1)).strftime('%Y-%m-%d')
res = (pd.to_datetime(x) + pd.Timedelta('1 day')).strftime('%Y-%m-%d')
print(res)
# 2017-05-16
Using pd.to_datetime, pd.TimeDelta and strftime:
fmt = '%Y-%m-%d'
(pd.to_datetime(<your series or column>, format=fmt) + pd.Timedelta('1 days')).dt.strftime(date_format=fmt)
Example
df = pd.DataFrame({'date': ['2017-04-02', '2017-04-23']})
fmt = '%Y-%m-%d'
>>> (pd.to_datetime(df.date, format=fmt) + pd.Timedelta('1 days')).dt.strftime(date_format=fmt)
0 2017-04-03
1 2017-04-24
Name: date, dtype: object
You can perform arithmetic operations with datetime and timedelta objects.
from datetime import datetime, timedelta
d = datetime(year=2018, month=3, day=1)
t = timedelta(days=1)
d + t
# datetime.datetime(2018, 3, 2, 0, 0)
d + t + t
# datetime.datetime(2018, 3, 3, 0, 0)
for i in range(30):
d += 1
print(d)
# datetime.datetime(2018, 3, 31, 0, 0)
You mean like this
date = datetime.date(2015,5,15)
date += datetime.timedelta(days=1)
It depends on what you mean by quickest. I'm assuming you mean the quickest way to program this, not the shortest program execution time (which might be relevant when you're doing lots of these).
from datetime import datetime, timedelta
original_date = '2018-5-15'
print('The original date is {}, the date one day later is: {}'.
format(original_date, (datetime.strptime(original_date, '%Y-%m-%d') +
timedelta(days=1)).strftime('%Y-%m-%d')
Step by step version:
Create a datetime object from the string, note the string that shows python the formatting (see the documentation for more information)
dt = datetime.strptime(original_date, '%Y-%m-%d')
Add a day
dt += timedelta(days=1)
Reformat it back to the requested string
print(dt.strftime('%Y-%m-%d'))
That's all!

Function to calculate excel date in python

I have two dates in a csv file 9:20:00 AM and 4:09:21 PM and I need to read them in python. How do I find the time between these two dates?
There are various ways of reading CSV's in python. I would suggest you take a look at the official python csv library and pandas and its easy to use read_csv function.
As for finding out the time difference, you will have to parse the strings and find the difference like this:
from dateutil.parser import parse
a = "9:20:00 AM"
b = "4:09:21 PM"
a_obj = parse(a)
b_obj = parse(b)
time_diff = b_obj - a_obj
print time_diff.total_seconds()
You can use csv package of python to read and write csv files..
For example..
import csv
with open('eggs.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile)
for row in spamreader:
#Your Logic
Using the package read the two dates. This may be string, and convert it into python date object and then find the time between two dates.
a = first_date
b = second_date
c = b - a
divmod(c.days * 86400 + c.seconds, 60)
You would be interested in datetime module.
Try this in your interpreter.
>>> from datetime import datetime
>>> a = datetime.strptime('9:20:00 AM','%I:%M:%S %p')
>>> a
datetime.datetime(1900, 1, 1, 9, 20)
>>> b = datetime.strptime('4:09:21 PM','%I:%M:%S %p')
>>> b
datetime.datetime(1900, 1, 1, 16, 9, 21)
>>> c= b-a
>>> c
datetime.timedelta(0, 24561)
So your '9:20:00 AM' was converted into a datetime object. Similarly the other time.
NOTE: '%I:%M:%S %p' This is the format in which you tell strptime to convert the string into a datetime object. After you perform operations on the datetime objects you get a timedelta object.
datetime.timedelta(0, 24561)
You can get the appropriate days,seconds from it. However you can't get hours,minutes for that you have to perform simple maths
Here is a code for you,
import datetime
def days_hours_minutes(td):
return td.days, td.seconds//3600, (td.seconds//60)%60
a = datetime.datetime.strptime('9:20:00 AM','%I:%M:%S %p')
b = datetime.datetime.strptime('4:09:21 PM','%I:%M:%S %p')
c = datetime.timedelta(0, 24561)
days,hours,minutes = days_hours_minutes(c)
print("{0}:Days,{1}:Hours,{2}:Minutes".format(days,hours,minutes))
output:
0:Days,6:Hours,49:Minutes
I have defined a function days_hours_minutes() to print the days, hours,minutes.

Delta time string to datetime object

Given a string that looks like "Hours:5 Minutes:34 Seconds:28" or "Minutes:34 Seconds:28", is there any pythonic way to convert it to a datetime object? I do not want to use a regex if there's an easier way.
Yes, there is. You can do it like this:
import time
datetime_string = "Hours:5 Minutes:34 Seconds:28"
if "Hours" in datetime_string:
datetime_object = time.strptime(datetime_string, "Hours:%H Minutes:%M Seconds:%S")
elif "Minutes" in datetime_string:
datetime_object = time.strptime(datetime_string, "Minutes:%M Seconds:%S")
else:
datetime_object = time.strptime(datetime_string, "Seconds:%S")
Note: When You create datetime object, values that You do not provide will be filled with default values.So, in case datetime_string contains only seconds, hours and minutes will be set to 0.
You may use datetime.strptime() to convert string into datetime object as:
>>> from datetime import datetime
>>> date_object = datetime.strptime('Hours:5 Minutes:34 Seconds:28', 'Hours:%H Minutes:%M Seconds:%S')
>>> date_object
datetime.datetime(1900, 1, 1, 5, 34, 28)
# ^ ^ ^
# Hour Min Seconds
Since you do not have date in the string, it will keep the default date of 1 Jan 1990. I think what you need is datetime.time() which return time object with same hour, minute, second and microsecond as in you datetime object. (tzinfo is None). For example:
>>> date_object.time()
datetime.time(5, 34, 28)
# ^ ^ ^
# Hour Min Seconds
where date_object is of datetime type created earlier.

Convert the unicode to datetime format

A function returns date and time in unicode format.
u'2014-03-06T04:38:51Z'
I wish to convert this to date and time format and subtract it with current datetime to get the number of days in between.
Thanks in advance
Check string is unicode
>>> import types
>>> type(u'2014-03-06T04:38:51Z') is types.UnicodeType
True
Converting strings to datetime:
>>> import datetime
>>> datetime.datetime.strptime(u'2014-03-06T04:38:51Z', '%Y-%m-%dT%H:%M:%SZ')
datetime.datetime(2014, 3, 6, 4, 38, 51)
Subtract from today to
>>> import datetime
>>> today = datetime.datetime.today()
>>> yourdate = datetime.datetime.strptime(u'2014-03-06T04:38:51Z', '%Y-%m-%dT%H:%M:%SZ')
>>> difference = today - yourdate
print str(difference)
First you have to convert your string to a datetime.datetime object.
import datetime
then = datetime.datetime.strptime(u'2014-03-06T04:38:51Z', "%Y-%m-%dT%H:%M:%SZ")
then represents itself as datetime.datetime(2014, 3, 6, 4, 38, 51), which looks about right. Then you have to get today's date as a datetime.datetime.
now = datetime.datetime.now()
Finally subtract it from your date (or vice versa - the question didn't make it clear).delta is a datetime.timedelta object that stores increments in days, seconds and microseconds. The latter two are always positive, the first can be negative.
for delta in (now-then, then-now):
print(delta, "::", delta.days, delta.seconds, delta.microseconds)
This prints out:
-1 day, 20:18:14.250142 :: -1 73094 250142
3:41:45.749858 :: 0 13305 749858
Best try it with a few examples to convince yourself it's correct.

Date Time Formats in Python

What are these date-time formats? I need to convert them to the same format, to check if they are the same. These are just two coming from a separate data source, so I need to find a way to make them the same format. Any ideas?
2013-07-12T07:00:00Z
2013-07-10T11:00:00.000Z
Thanks in advance
That extra .000 is micro seconds.
This will convert a date string of a format to datetime object.
import datetime
d1 = datetime.datetime.strptime("2013-07-12T07:00:00Z","%Y-%m-%dT%H:%M:%SZ")
d2 = datetime.datetime.strptime("2013-07-10T11:00:00.000Z","%Y-%m-%dT%H:%M:%S.%fZ")
Then convert them into any format depending on your requirement, by using:
new_format = "%Y-%m-%d"
d1.strftime(new_format)
perhaps use .isoformat()
string in ISO 8601 format, YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM]
>>> import datetime
>>> datetime.datetime.utcnow().isoformat() + "Z"
'2013-07-11T22:26:51.564000Z'
>>>
Z specifies "zulu" time or UTC.
You can also add the timezone component by making your datetime object timezone aware by applying the appropriate tzinfo object. With the tzinfo applied the .isoformat() method will include the appropriate utc offset in the output:
>>> d = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
>>> d.isoformat()
'2019-11-11T00:52:43.349356+00:00'
You can remove the microseconds by change the microseconds value to 0:
>>> no_ms = d.replace(microsecond=0)
>>> no_ms.isoformat()
'2019-11-11T00:52:43+00:00'
Also, as of python 3.7 the .fromisoformat() method is available to load an iso formatted datetime string into a python datetime object:
>>> datetime.datetime.fromisoformat('2019-11-11T00:52:43+00:00')
datetime.datetime(2019, 11, 11, 0, 52, 43, tzinfo=datetime.timezone.utc)
http://www.ietf.org/rfc/rfc3339.txt
you can try to trim the string
data = "2019-10-22T00:00:00.000-05:00"
result1 = datetime.datetime.strptime(data[0:19],"%Y-%m-%dT%H:%M:%S")
result2 = datetime.datetime.strptime(data[0:23],"%Y-%m-%dT%H:%M:%S.%f")
result3 = datetime.datetime.strptime(data[0:9], "%Y-%m-%d")
use datetime module.
For a variable
import datetime
def convertDate(d):
new_date = datetime.datetime.strptime(d,"%Y-%m-%dT%H:%M:%S.%fZ")
return new_date.date()
convertDate("2019-12-23T00:00:00.000Z")
you can change the ".date()" to ".year", ".month", ".day" etc...
Output: # is now a datetime object
datetime.date(2019, 12, 23)
For a DataFrame column, use apply()
df['new_column'] = df['date_column'].apply(convertDate)
* Short and best way:
str(datetime.datetime.now()).replace(' ','T')
or
str(datetime.datetime.now()).replace(' ','T') + "Z"

Categories

Resources