Comparing dates and times in different formats using Python - python

I have lines of the following format in a file.
Summary;meeting;Description;None;DateStart;20100629T110000;DateEnd;20100629T120000;Time;20100805T084547Z
I need to create a function that has two inputs: time and date in the following formats Time: HH:MM and date as mmddyyyy. (These are strings). Now the function needs to read this line and see if the the input date and time, lies between DateStart(20100629T11000) and DateEnd(20100629T120000). How do i deal with this since the format of date and time in the input and the line are in two formats?

You can parse a string into a datetime with strptime:
>>> datetime.datetime.strptime('20100629T110000', '%Y%m%dT%H%M%S')
datetime.datetime(2010, 6, 29, 11, 0)
>>> datetime.datetime.strptime('23:45 06192005', '%H:%M %m%d%Y')
datetime.datetime(2005, 6, 19, 23, 45)
And then you can compare (<, <=, etc) the two datetimes.

To handle dates and times, use Python's datetime module. The datetime class in that module has a method for reading datetimes from strings, called strptime. So you can do:
# read the file, so that:
strStart = "20100629T110000"
strEnd = "20100629T120000"
imTime = "HH:MM"
inDate = "mmddyyyy"
import datetime
dateStart = datetime.datetime.strptime( strStart, "%Y%m%dT%H%M%S" )
dateEnd = datetime.datetime.strptime( strEnd, "%Y%m%dT%H%M%S" )
dateIn = datetime.datetime.strptime( inDate + inTime, "%m%d%Y%H:%M" )
assert dateStart < dateIn < dateEnd
N.B. You can use csv to read the file.

Use the datetime class inside the datetime module. Here is a function that does what you need, though you might need to adjust boundary conditions:
from datetime import datetime
def f(row, datestr, timestr):
tmp = row.split(";")
start = datetime.strptime(tmp[5], "%Y%m%dT%H%M%S")
end = datetime.strptime(tmp[7], "%Y%m%dT%H%M%S")
mytimestamp = datetime.strptime(datestr+timestr, "%d%m%Y%H:%M")
if (start < mytimestamp and mytimestamp < end):
print "inside"
else:
print "not inside"
>>> f("Summary;meeting;Description;None;DateStart;20100629T110000;DateEnd;20100629T120000;Time;20100805T084547Z", "29062010", "11:00")
not inside
>>> f("Summary;meeting;Description;None;DateStart;20100629T110000;DateEnd;20100629T120000;Time;20100805T084547Z", "29062010", "11:30")
inside

Related

How do I convert YYYYMMDD to datetime, but maintain the same format

I have an variable integer that is in the format YYYYMMDD.
How do i convert that variable to datetime while maintaining the format of YYYYMMDD?
date = 20200930
nextday = date + 1
How do i fix this, so the nextday variable displays as 20201001
If I'm understanding what you need to do correctly, you can easily do it using the datetime package.
First, convert your variable to a date:
import datetime
date = 20200930
dt = datetime.datetime.strptime(str(date), '%Y%m%d')
Now, when you print this, you will see:
datetime.datetime(2020, 9, 30, 0, 0).
You can then add a day to it:
dt_new = dt + datetime.timedelta(1)
And specify the format you want to see the new date variable in:
print ('{date: %Y%m%d}'.format(date=dt_new))
which will give:
20201001.

python json date object to python datetime

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'

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.

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"

Converting (YYYY-MM-DD-HH:MM:SS) date time

I want to convert a string like this "29-Apr-2013-15:59:02"
into something more usable.
The dashes can be easily replaced with spaces or other characters. This format would be ideal: "YYYYMMDD HH:mm:ss (20130429 15:59:02)".
Edit:
Sorry, I did not specifically see the answer in another post. But again, I'm ignorant so could have been looking at the solution and didn't know it. I've got this working, but I wouldn't consider it "pretty."
#29-Apr-2013-15:59:02
import sys, datetime, time
#inDate = sys.argv[1]
inDate = 29-Apr-2013-15:59:02
def getMonth(month):
monthDict = {'Jan':'01','Feb':'02','Mar':'03','Apr':'04','May':'05','Jun':'06','Jul':'07','Aug':'08','Sep':'09','Oct':'10','Nov':'11','Dec':'12'}
for k, v in monthDict.iteritems():
if month == k:
return v
day = inDate[:2]
#print day
month = inDate[3:6]
#print month
year = inDate[7:11]
#print year
time = inDate[-8:]
#print time
newDate = year+getMonth(month)+day
newDateTime = newDate+" "+time
print newDate
print newDateTime
Any thoughts on improving?
Use datetime.strptime() to parse the inDate string into a date object, use datetime.strftime() to output in whatever format you like:
>>> from datetime import datetime
>>> inDate = "29-Apr-2013-15:59:02"
>>> d = datetime.strptime(inDate, "%d-%b-%Y-%H:%M:%S")
>>> d
datetime.datetime(2013, 4, 29, 15, 59, 2)
>>> d.strftime("YYYYMMDD HH:mm:ss (%Y%m%d %H:%M:%S)")
'YYYYMMDD HH:mm:ss (20130429 15:59:02)'
Have you investigated dateutil?
http://labix.org/python-dateutil
I found a similar question to yours:
How do I translate a ISO 8601 datetime string into a Python datetime object?
You want to look into datetime, in particular strptime.

Categories

Resources