Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get day name from datetime

How can I get the day name (such as Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday) from a datetime object in Python?

So, for example, datetime(2019, 9, 6, 11, 33, 0) should give me "Friday".

like image 743
gadss Avatar asked Dec 05 '11 02:12

gadss


People also ask

How can I get the day from a datetime?

Use the strftime() method of a datetime module to get the day's name in English in Python. It uses some standard directives to represent a datetime in a string format. The %A directive returns the full name of the weekday. Like, Monday, Tuesday.

How do I get the day in Python?

Use the weekday() Method to Get the Name of the Day in Python. In Python, weekday() can be used to retrieve the day of the week. The datetime. today() method returns the current date, and the weekday() method returns the day of the week as an integer where Monday is indexed as 0 and Sunday is 6.

How do I convert datetime to days in Python?

Alternatively using from time import time : time() // (24 * 60 * 60) .

How do I return the weekday name in Python?

You can use index number like this: days=["sunday","monday"," Tuesday", "Wednesday" ,"Thursday", "Friday", "Saturday"] def date(i): return days[i] print (date(int(input("input index. "))))


2 Answers

import datetime now = datetime.datetime.now() print(now.strftime("%A")) 

See the Python docs for datetime.now, datetime.strftime and more on strftime.

like image 171
Matt Joiner Avatar answered Sep 29 '22 21:09

Matt Joiner


>>> from datetime import datetime as date >>> date.today().strftime("%A") 'Monday' 
like image 36
Abhijit Avatar answered Sep 29 '22 21:09

Abhijit