Python3: Convert arbitrary date to day of the week

Related searches

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?

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}")

8.1. datetime — Basic date and time types — Python 3.4.10 , The smallest year number allowed in a date or datetime object. Return the day of the week as an integer, where Monday is 0 and Sunday is 6. The standard library has timezone class for handling arbitrary fixed offsets from UTC and� There are many ways to find the day of a week for a given date. In this article, to find the day of the week for a given date, we use the strptime (), strftime () or weekday () function. Here are some examples to find the day of the week for a given date. Example 1: By Using strptime () function

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)

datetime — Basic date and time types — Python 3.8.5 documentation, The datetime module supplies classes for manipulating dates and times. Return the day of the week as an integer, where Monday is 0 and Sunday is 6. This does not support parsing arbitrary ISO 8601 strings - it is only intended as the� Last Updated: 26-01-2019 Write a Python program to find the day of the week for any particular date in the past or future. Let the input be in the format “dd mm yyyy”.

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

dateparser – python parser for human readable dates — DateParser , Generic parsing of relative dates like: '1 min ago' , '2 weeks ago' , '3 months, 1 week and 1 day ago' , 'in 2 days' , 'tomorrow' . Generic parsing of dates with time� Weekday_name is the inbuilt method for finding the day of the week in pandas python. Also dayofweek function in pandas is used for getting the day of the week in numbers. Lets see how to Get the Day of the week from date in English in pandas

Python: ISO 8601 date – Jingwen Zheng – Data Science Enthusiast, date by python such as ISO year, ISO week, ISO date; how to convert datetime the first week with the majority (four or more) of its days in the starting Caution: This does not support parsing arbitrary ISO 8601 strings - it is� Solved: Trying to convert a date to day of the week. So field of actual date to new variable that is Monday, Tuesday, Wednesday, etc based on the

Using Python datetime to Work With Dates and Times – Real Python, In this tutorial, you'll learn all about the built-in Python datetime library. Prior to 2007, clocks were set forward on the first Sunday in April and set back determine the time by calculating the number of seconds from an arbitrary date. This means the format is year, month, day, hour, minute, and second:. Return a date corresponding to the ISO calendar date specified by year, week and day. This is the inverse of the function date.isocalendar (). New in version 3.8.

The weekday () function of date class in datetime module, returns an integer corresponding to the day of the week. Here is the mapping of integer values to the days of the week.

Comments
  • Can you add a minimal reproducible example?
  • Check out this post
  • You can just make a list or dict with the 7 days of the week and check if one of them is present as text. Or is the day of the week not always present as text?