Search code examples
pythondatetimetimestrftime

how do i convert format of current time in python


i have been trying to use 'strftime()' with '%j' to get the current day's number of the year (0-365):

daynow = ("%d"% (datetime.day))
daynum =  (time.strftime("%j", time.gmtime(daynow)))

print (daynum)

but returns: Exception has occurred: TypeError %d format: a number is required, not getset_descriptor

what does this mean, what is a getset_descriptor and how can i fix this?


Solution

  • Try the below code to get day of the month, day of the year and week of the year :

    from datetime import datetime,date
    today = datetime.now()
    day_of_the_month = ("%d"% (today.day))
    day_of_year = today.timetuple().tm_yday
    week_of_the_year = today.isocalendar()[1]