Python datetime module provide the complete information date and time.
Below is the example to get date and time using datetime module.
datetime module is having below classes which get use most of the time:
This datetime object is having datetime class to get date and time.
datetime.datetime.now() will give current date and time.
Example
OUTPUT
using datetime object we can set date and time to use further.
datetime(year, month, date, hour, minute, second, microsecond)
OUTPUT:
Using date time object we can find out the current date, month, year also as show in below example.
OUTPUT:
There are some other attributes from date class to get date, month and year separately.
OUTPUT:
Python allows us to create custom date object with user defined values.
OUTPUT
2022–09–28 00:00:00
Using date time object we can find out the current time, hour, minutes, second and microsecond.
OUTPUT:
Python provide us the capability to print data and time in specified format.
Below are the conventions we use to format the code.
OUTPUT:
%P is use to print time having AM or PM.
OUTPUT:
Date Time = 12:19:04 PM
Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.
📝 Blogs
javadoubts.com © All rights reserved