1

I am trying to convert dates of the following format:

2007-10-18 11:31:46 -0400 (Thu, 18 Oct 2007)

or

Thu, 18 Oct 2007 11:31:49 -0400

to the day of the week. Even though the day is given in the above dates, but how can I extract the only day of the week from the above dates?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Aiguo
  • 3,416
  • 7
  • 27
  • 52

3 Answers3

0

Is this too simple:

days = {
    'mon': 'Monday',
    'thu': 'Thursday',
    # and the others
}

haystack = '2007-10-18 11:31:46 -0400 (Thu, 18 Oct 2007)'
for acronym, full_name in days.items():
    if acronym in haystack.lower():
        print(f"Found {full_name}")
3UqU57GnaX
  • 389
  • 3
  • 12
0
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
date = "2007-10-18 11:31:46 -0400 (Thu, 18 Oct 2007)"
for d in days:
    if d in date:
         print(d)
Adarsh Chavakula
  • 1,509
  • 19
  • 28
0

You can use dateutil.parser to get a datetime object from a string:

from dateutil import parser
dt = parser.parse("2007-10-18 11:31:46 -0400")
# dt = datetime.datetime(2007, 10, 18, 11, 31, 46, tzinfo=tzoffset(None, -14400))

Then just use datetime.weekday. This will give you a number from 0 to 6, where 0 is the first day of the week relatively to your timezone, Monday by default.

dt.weekday
# 3, starting from 0 so it's Thursday
Right leg
  • 16,080
  • 7
  • 48
  • 81