pythondatetimepython-datetime

How can i fetch given day of any given year?


I'm working on a local program in which I want the first day of the given number of year like(Monday, Tuesday etc). How can I have it?

firstly I try datetime module for this purpose but it gives current date. But i don't want current date or day. I want day of given year.


Solution

  • To get the first day of the year, set month to 1 and day to 1, then use %A to format the date with only the weekday

    from datetime import date
    
    year = 1997
    d = date(year, 1, 1)
    print(d.strftime("%A"))