I'm trying to find the difference between these two dates or any other dates for that matter
a = '11-Feb-2003'
b = '28-Aug-2015'
I'm looking for a way (something not so manual/diy) to convert the months (Feb & August) into integers?
Should I create a dictionary?
To be even more specific I'd like to turn the dates into lists.
'11-Feb-2003' becomes [11, 2, 2003] = startdate
'28-Aug-2015' becomes [28, 8, 2015] = enddate
From there I can calculate the difference in number of days between the two by executing:
import datetime
datetime.date(enddate[2], enddate[1], enddate[0]) -
dateimdate(startdate[2], startdate[1], startdate[0])
You can use Python's datetime module to convert a string to a date object.
The strptime function takes a string and the format of the date.
from datetime import datetime
print datetime.strptime('11-Feb-2003', '%d-%b-%Y')
%d is for Day of the month as a zero-padded decimal number. (01, 02, ..., 31)
%b is for Month as locale’s abbreviated name. (Jan, Feb, ..., Dec)
%Y is for Year with century as a decimal number. (1970, 1988, 2001, 2013)
Here is a list of all the format specifiers for your reference.
To find the difference between two strings that represent dates, you first need to convert them to Python's date type, then simply subtract them:
>>> import datetime
>>> s1 = '11-Feb-2003'
>>> s2 = '28-Aug-2015'
>>> d1 = datetime.datetime.strptime(s1, '%d-%b-%Y')
>>> d2 = datetime.datetime.strptime(s2, '%d-%b-%Y')
Now, once your subtract the two datetime objects, you'll get a special datetime.timedelta object:
>>> i = d2-d1
>>> i
datetime.timedelta(4581)
You can get a friendly representation of the difference if you print the object (or convert it to a string):
>>> print(i)
4581 days, 0:00:00
You can also query the object, for example:
>>> i.days
4581
You can use strptime.
import datetime as d
d.datetime.strptime(a, '%d-%b-%Y')
#=> datetime.datetime(2003, 2, 11, 0, 0)
you can use time module:
>>> import time
>>> a = '11-Feb-2003'
>>> b = '28-Aug-2015'
>>> time.strptime(a, "%d-%b-%Y") # %b Locale’s abbreviated month name.
time.struct_time(tm_year=2003, tm_mon=2, tm_mday=11, tm_hour=0,tm_min=0, tm_sec=0, tm_wday=1, tm_yday=42, tm_isdst=-1)
>>> my_date = time.strptime(a, "%d-%b-%Y")
>>> "{}-{}-{}".format(my_date.tm_mday, my_date.tm_mon, my_date.tm_year)
'11-2-2003'
You can use the split function:
a.split("-")
and that will return:
["11", "Feb", "2003"]
import datetime
datetime.datetime.strptime(month, '%B').month
Use %B for full name of the month (e.g. January), %b for short version (e.g. Jan)
Related
I have dates in the form 26/11/2015. How can I convert them into the format 26-Nov-2015 and still keep them as dates and not strings?
Your question does not make much sense. If you keep them as dates, they have no format. The format is only manifested when you convert them to strings.
So the answer is: Store the dates as date (or datetime) objects, and use datetime.strftime with some specific format whenever you need them as a string:
>>> from datetime import date
>>> d = date(2016, 11, 26)
>>> d.strftime("%Y/%m/%d")
'2016/11/26'
>>> d.strftime("%d-%b-%Y")
'26-Nov-2016'
Conversely, use strptime to parse strings in different formats to dates:
>>> datetime.datetime.strptime("26-Nov-2015", "%d-%b-%Y")
datetime.datetime(2015, 11, 26, 0, 0)
from datetime import datetime
date = datetime.strptime('26/11/2015', '%d/%m/%Y')
print date.strftime("%d-%B-%Y")
In the above example, we are taking your input string 'dd/mm/yyyy' and turning it into a python datetime saving it to a variable called date (for future usage as per your request), and then printing it out in the format requested.
You want to use the datetime module I think. For example:
from datetime import date
a = date(2015, 11, 26)
a.strftime("%A %d of %B, %Y")
should give you 'Thursday 26 of November, 2015'
Or for your specific formatting request:
a.strftime("%d-%b-%Y") #'26-Nov-2015'
Hope this helps, good luck!
I'm pulling a timestamp that looks like this - 2014-02-03T19:24:07Z
I'm trying to calculate the number of days since January 1.
I was able to convert it to datetime using
yourdate = dateutil.parser.parse(timestamp)
But now I'm trying to parse it and grab individual elements, such as the month & day.
Is there a way to convert it to strptime so I can select each element?
Just access the month, day using year, month, day attributes..
>>> import dateutil.parser
>>> yourdate = dateutil.parser.parse('2014-02-03T19:24:07Z')
>>> yourdate.year
2014
>>> yourdate.month
2
>>> yourdate.day
3
Just to be a little more complete:
>>> from dateutil.parser import parse
>>> from datetime import datetime
>>> import pytz
>>> d = parse('2014-02-03T19:24:07Z')
>>> other = datetime(year=2014, month=1, day=1, tzinfo=pytz.utc)
>>> (d-other).days
33
You have to make sure the other datetime is timezone aware if you're creating it with datetime as opposed to the datetime you're parsing with dateutil.
There's no need for converting. The resulting datetime.datetime object has all necessary properties which you can access directly. For example:
>>> import dateutil.parser
>>> timestamp="2014-02-03T19:24:07Z"
>>> yourdate = dateutil.parser.parse(timestamp)
>>> yourdate.day
3
>>> yourdate.month
2
See: https://docs.python.org/2/library/datetime.html#datetime-objects
if you want to calculate:
import dateutil.parser
yourdate = dateutil.parser.parse('2014-02-03T19:24:07Z')
startdate = dateutil.parser.parse('2014-01-01T00:00:00Z')
print (yourdate - startdate)
Another way to solve without the dateutil module:
import datetime
# start date for comparision
start = datetime.date(2014, 1, 1)
# timestamp as string
datefmt = "%Y-%m-%dT%H:%M:%SZ"
current = "2014-02-03T19:24:07Z"
# convert timestamp string to date, dropping time
end = datetime.datetime.strptime(current, datefmt).date()
# compare dates and get number of days from timedelta object
days = (end - start).days
This assumes you don't care about time (including timezones).
Say I have a week number of a given year (e.g. week number 6 of 2014).
How can I convert this to the date of the Monday that starts that week?
One brute force solution I thought of would be to go through all Mondays of the year:
date1 = datetime.date(1,1,2014)
date2 = datetime.date(12,31,2014)
def monday_range(date1,date2):
while date1 < date2:
if date1.weekday() == 0:
yield date1
date1 = date1 + timedelta(days=1)
and store a hash from the first to the last Monday of the year, but this wouldn't do it, since, the first week of the year may not contain a Monday.
You could just feed the data into time.asctime().
>>> import time
>>> week = 6
>>> year = 2014
>>> atime = time.asctime(time.strptime('{} {} 1'.format(year, week), '%Y %W %w'))
>>> atime
'Mon Feb 10 00:00:00 2014'
EDIT:
To convert this to a datetime.date object:
>>> datetime.datetime.fromtimestamp(time.mktime(atime)).date()
datetime.date(2014, 2, 10)
All about strptime \ strftime:
https://docs.python.org/2/library/datetime.html
mytime.strftime('%U') #for W\C Monday
mytime.strftime('%W') #for W\C Sunday
Sorry wrong way around
from datetime import datetime
mytime=datetime.strptime('2012W6 MON'. '%YW%U %a')
Strptime needs to see both the year and the weekday to do this. I'm assuming you've got weekly data so just add 'mon' to the end of the string.
Enjoy
A simple function to get the Monday, given a date.
def get_monday(dte):
return dte - datetime.timedelta(days = dte.weekday())
Some sample output:
>>> get_monday(date1)
datetime.date(2013, 12, 30)
>>> get_monday(date2)
datetime.date(2014, 12, 29)
Call this function within your loop.
We can just add the number of weeks to the first day of the year.
>>> import datetime
>>> from dateutil.relativedelta import relativedelta
>>> week = 40
>>> year = 2019
>>> date = datetime.date(year,1,1)+relativedelta(weeks=+week)
>>> date
datetime.date(2019, 10, 8)
To piggyback and give a different version of the answer #anon582847382 gave, you can do something like the below code if you're creating a function for it and the week number is given like "11-2023":
import time
from datetime import datetime
def get_date_from_week_number(str_value):
temp_str = time.asctime(time.strptime('{} {} 1'.format(str_value[3:7], str_value[0:2]), '%Y %W %w'))
return datetime.strptime(temp_str, '%a %b %d %H:%M:%S %Y').date()
In Sweden we sometimes use a strange date format, for example New Year is the 31/12. If I have this format as a string ,which can be any date between 1/1 and 31/12, and if we assume that it is this year, how do I get into a standard date format using Python (format as 2012-01-01 and 2012-12-31) that can be sore in be stored as a date in a mySQL database.
Simply split the two values, map them to integers and update a datetime.date() instance:
import datetime
day, month = map(int, yourvalue.split('/'))
adate = datetime.date.today().replace(month=month, day=day)
By using datetime.date.today() we get the current year.
Demo:
>>> import datetime
>>> somevalue = '31/12'
>>> day, month = map(int, somevalue.split('/'))
>>> datetime.date.today().replace(month=month, day=day)
datetime.date(2012, 12, 31)
>>> someothervalue = '1/1'
>>> day, month = map(int, someothervalue.split('/'))
>>> datetime.date.today().replace(month=month, day=day)
datetime.date(2012, 1, 1)
Alternatively, you could use the datetime.strptime() method to parse these dates, but you'll have to manually correct the year afterwards (it'll use 1900 as the default if no year is being parsed):
adate = datetime.datetime.strptime(yourvalue, '%d/%m').date()
adate = adate.replace(year=datetime.date.today().year)
There is nothing strange about this format :)
You can use the datetime module:
import datetime
d = datetime.datetime.strptime('31/12', '%d/%m').date().replace(year=2012)
print d
>> datetime.date(2012, 12, 31)
If I have lets say this string "2008-12-12 19:21:10" how can I convert it into a date and get the year, month and day from that created object separately?
Use the datetime.datetime.strptime() function:
from datetime import datetime
dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')
Now you have a datetime.datetime object, and it has .year, .month and .day attributes:
>>> from datetime import datetime
>>> datestring = "2008-12-12 19:21:10"
>>> dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')
>>> print dt.year, dt.month, dt.day
2008 12 12
https://www.tutorialspoint.com/python/time_strptime.htm
Here you can find strptime() method complete description. where you can find all type of strings.
Eg:- To convert string like this '15-MAY-12'
>>>from datetime import datetime
>>>datestring = "15-MAY-12"
>>>dt = datetime.strptime(datestring, '%d-%b-%Y')
>>>print(dt.year, dt.month, dt.day)
2012 MAY 15
with milliseconds
>>> from datetime import datetime
>>> datestring = "2018-04-11 23:36:18.886585"
>>> dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S.%f')
>>> print dt.year, dt.month, dt.day
2018 04 11
One thing to add; pay attention that %y is for two digit year notation %Y is for the four digit one:
import datetime
datestring = '15-MAY-12'
print(datetime.datetime.strptime(datestring, '%d-%b-%y'))
>>> datetime.datetime(2012, 5, 15, 0, 0)
datestring = '15-MAY-2012'
print(datetime.datetime.strptime(datestring, '%d-%b-%Y'))
>>> datetime.datetime(2012, 5, 15, 0, 0)