python strptime ignoring %A? - python

I have some arbitrary-week times in the following format: "Monday 11:30am"
I would like to parse these into datetime.datetimes. I don't care what year/month/day I end up with in the object, but if the string says "Monday", then the resulting datetime.datetime should be a day that is a Monday.
Unfortunately, the %A formatter (day of week) seems to be ignored by strptime:
>>> import datetime
>>> a = "Monday 11:30am"
>>> b = "Tuesday 11:30am"
>>> dt_format = "%A %I:%M%p"
>>> datetime.datetime.strptime(a, dt_format)
datetime.datetime(1900, 1, 1, 11, 30)
>>> datetime.datetime.strptime(b, dt_format)
datetime.datetime(1900, 1, 1, 11, 30)
How do I correctly parse the day of the week?

It seems that datetime is not picking up a date correctly when only specifying the day of the week.
Since you do not care what year/month/day you end up with, you can use the current week number of the year. When combined with the day and week number, datetime will output a date correctly.
import datetime
import calendar
rand_date = datetime.datetime.strftime(datetime.datetime.today(), '%U %Y ')
dt_format = "%U %Y %A %I:%M%p"
a = "Monday 11:30am"
b = "Tuesday 11:30am"
print(datetime.datetime.strptime(rand_date + a, dt_format))
print(calendar.day_name[datetime.datetime.strptime(rand_date + a, dt_format).weekday()])
print(datetime.datetime.strptime(rand_date + b, dt_format))
print(calendar.day_name[datetime.datetime.strptime(rand_date + b, dt_format).weekday()])
2021-02-22 11:30:00
Monday
2021-02-23 11:30:00
Tuesday

Related

Difference between two timestamps with different weekdays?

I've the following dates:
Tue 19:00-20:00
Wed 04:25-15:14
How can i get the difference between Tue 20:00 and Wed 04:25?
I've tried to transform it in datetime objects but since i don't have the day of the month or a year both objects will get the same date:
d1 = datetime.strptime("Tue 20:00", "%a %H:%M")
d2 = datetime.strptime("Wed 04:25", "%a %H:%M")
d1 is: datetime.datetime(1900, 1, 1, 20, 0)
d2 is: datetime.datetime(1900, 1, 1, 4, 25)
The difference in minutes should be 505 but in this case will be -935.0 because they have the same date, and so they differ by almost 16 hours and not about 8 and a half:
diff = (d2 - d1).total_seconds() / 60 # -935.0
datetime.strptime() can't do anything else, because the day of the week is not enough to form a date. You'd need a week number and a year for a week-day to make any sense. If you need to get the relative difference between two dates that are assumed to be in the same week, then any week number and year would do, e.g.:
arbitrary_iso_week = "2020-25 "
d1 = datetime.strptime(arbitrary_iso_week + "Tue 20:00", "%G-%V %a %H:%M")
d2 = datetime.strptime(arbitrary_iso_week + "Wed 04:25", "%G-%V %a %H:%M")
This makes use of the %G and %V formatters for ISO 8601 week numbers.
This produces datetime objects with a more meaningful date releationship:
>>> arbitrary_iso_week = "2020-25 "
>>> d1 = datetime.strptime(arbitrary_iso_week + "Tue 20:00", "%G-%V %a %H:%M")
>>> d2 = datetime.strptime(arbitrary_iso_week + "Wed 04:25", "%G-%V %a %H:%M")
>>> d1
datetime.datetime(2020, 6, 16, 20, 0)
>>> d2
datetime.datetime(2020, 6, 17, 4, 25)
>>> (d2 - d1).total_seconds() / 60
505.0
The actual dates don't matter here, just their difference in days. d1 is now earlier, not later, than d2.
You may have to take into account that week days are circular, in that you could have an earlier date on, say, Friday, and a later date on Monday. If you can assume that the first value must be earlier than the second, then the solution is simple. Just subtract 7 days from d1 if it is later than d2:
# if the first date must come before the second, then it must be one week earlier.
if d1 > d2:
d1 -= timedelta(days=7)

Datetime from year and week number

I have a year and a week number which I want to convert into a datetime.datetiem object. My (naive?) reading of the documentation hinted that strptime('2016 00', '%Y %W') should do just that. However:
In [2]: from datetime import datetime
In [3]: datetime.strptime('2016 00', '%Y %W')
Out[3]: datetime(2016, 1, 1, 0, 0)
In [4]: datetime.strptime('2016 52', '%Y %W')
Out[4]: datetime(2016, 1, 1, 0, 0)
What am I doing wrong?
So it turns out that the week number isn't enough for strptime to get the date. Add a default day of the week to your string so it will work.
> from datetime import datetime
> myDate = "2016 51"
> datetime.strptime(myDate + ' 0', "%Y %W %w")
> datetime.datetime(2016, 12, 25, 0, 0)
The 0 tells it to pick the Sunday of that week, but you can change that in the range of 0 through 6 for each day.
From the docs (see note 7 at the bottom):
When used with the strptime() method, %U and %W are only used in
calculations when the day of the week and the year are specified.
Thus, as long as you don't specify the weekday, you will effectively get the same result as datetime.strptime('2016', '%Y').

How to convert 2015 June 1 into date format in python [duplicate]

This question already has answers here:
Convert string date into date format in python?
(3 answers)
Closed 7 years ago.
How to convert 2015 June 1 into date format in python like date_object = datetime.date(2014, 12, 4)
You can use the format - '%Y %B %d' along with datetime.datetime.strptime() method to convert string to date. Where %Y is 4 digit year, %B is complete month name, and %d is date.
Example/Demo -
>>> datetime.datetime.strptime('2015 June 1','%Y %B %d')
datetime.datetime(2015, 6, 1, 0, 0)
>>> datetime.datetime.strptime('2015 June 1','%Y %B %d').date()
datetime.date(2015, 6, 1)
Use the first one, if you are content with datetime object, if you want the date() object itself, you can use the second one.
You can use the date constructor
>>> from datetime import date
>>> date_object = date(year=2015, month=6, day=1)
>>> print date_object
2015-06-01

Define starting day in week using hour and not just date

I need to define a "long" week by hour in a day.
The week will start on Tuesday at 12AM and will end on next Tuesday at 6AM.
The next week will do an overlap (again starting from Tuesday at 12AM and so on).
Can this be done with isocalendar or strftime?
I think you need a timedelta, which is part of the standard datetime library:
>>> from datetime import datetime, timedelta
Given a starting date:
>>> start = datetime(year=2015, month=5, day=4, hour=0)
>>> start
datetime.datetime(2015, 5, 4, 0, 0)
You can use timedelta to store the difference between dates:
>>> long_week = timedelta(weeks=1, hours=6)
Add the delta to the start date:
>>> end = start + long_week
>>> end
datetime.datetime(2015, 5, 11, 6, 0)

How to get week start dates and week number of each week in a year considering start day of the week is Monday in python?

How can I get week start dates of each week in a year, considering start day of the week is Monday in python?
This assumes start day is Sunday:
>>>import datetime as datetime
>>>dt = datetime .date(2013,12,30)
>>>dt.isocalendar()[1]
1
However, result shouldn't be 1, because 30-12-2013 is still in 2013.
I don't think behaviour of isocalendar can be changed.
From : http://docs.python.org/2/library/datetime.html#datetime.date.isocalendar
The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday.
But strftime can display week number :
%U "All days in a new year preceding the first Sunday are considered to be in week 0."
%W "All days in a new year preceding the first Monday are considered to be in week 0."
>>> import datetime
>>> dt = datetime.date(2013,12,30)
>>> dt.isocalendar()
(2014, 1, 1)
>>> dt.strftime("%U")
'52'
But to respond to your first question, why don't you just use datetime.timedelta ?
>>> import datetime
>>> dt = datetime.date(2013,12,30)
>>> w = datetime.timedelta(weeks=1)
>>> dt - w
datetime.date(2013, 12, 23)
>>> dt + w
datetime.date(2014, 1, 6)
>>> dt + 10 * w
datetime.date(2014, 3, 10)

Categories

Resources