Python Tutorials
- Python
- How to install Python?
- PIP
- How to Run Python Program
- Python Identifiers,Statement, Indentation and Comments
- Variable
- Data type
- Decision Making
- Python Loops
- Break,Continue, Pass
- Functions
- Predefine Functions
- Lambda Functions
- Variable Scope
- List
- Tuple
- Python Sets
- Python Dictionary
- Python String
- String Formating
- Input/Output
- File Handling (Input / Output)
- Iterators
- Python Modules
- Python Date
- Python JSON
- Classes and Objects
- Constructor
- Polymorphism
- Encapsulation
- inheritance
- Class or Static Variables in Python
- class method vs static method
- Abstraction
- Exception Handling
- MySql Python
- MySql Create Database
- MySql CRUD
- Django
How to use Python Date?
A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.
File name : index.py
import datetime
x = datetime.datetime.now()
print(x)
output :-
2021-08-16 11:50:43.296824
Examle
Print day and year of the date
File name : index.py
import datetime
x = datetime.datetime.now()
print(x.year)
print(x.strftime("%A"))
Output :-
2021
Monday
Creating Date Objects
The datetime() class requires three parameters to create a date: year, month, day.
File name : index.py
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)
The strftime() Method
The datetime object has a method for formatting date objects into readable strings. The method is called strftime(), and takes one parameter, format, to specify the format of the returned string:
File name : index.py
import datetime
x = datetime.datetime(2018, 6, 1)
print(x.strftime("%B"))
output :-
June
Getting current time
File name : index.py
import time;
localtime = time.localtime(time.time())
print "Local current time :", localtime
Example 1: Python get current date
we imported the date class from the datetime module. Then, we used the date.today() method to get the current local date. By the way, date.today() returns a date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.
File name : index.py
from datetime import date
today = date.today()
print("Today's date:", today)
Example:-
File name : index.py
from datetime import date
today = date.today()
# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)
# Textual month, day and year
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)
# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)
# Month abbreviation, day and year
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)
Output :-
d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019
Get the current date and time
File name : index.py
from datetime import datetime
# datetime object containing current date and time
now = datetime.now()
print("now =", now)
# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)
output :-
now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56
File name : index.py
File name : index.py
File name : index.py
File name : index.py
File name : index.py