Python datetime example

Python datetime example: We get the imported datetime module by using the import datetime statement. The classes in defined datetime are the datetime class. The datetime class in python is as follows,
Date:-it manipulates date.
Time:-it is independent of day.
Datetime:-also called a combination of date and time.
Timedelta:-it is the duration of time used for manipulating the dates.
Tzinfo:-it is abstract class dealing with the time zone.

Example of getting current date and time:-


Import datetime
Print(datetime_object)
Output:-

2018-12-19 09:26:03.478039
We get the imported datetime module by using the import datetime statement.
The classes in defined datetime are the datetime class.
So datetime module is datetime class and used for method to create the datetime object and current local date and time.
An example is get current date:-
Import datetime
Date_object=datetime.date.today ()
Print(date_object)
Output:-
2018-12-19
We use the method today () and define date class and get date object containing the local date.

Date object that represents a date:-

Import datetime D=datetime (2019, 4, 13) Print (d) Output:- 2019-04-13

Get date from a timestamp:-

To create the date object from a timestamp.

It is a number of second between date and jan 1, 1970 at UTC.

Example:-

From datetime import date

Timestamp=date.fromtimestamp (1326244364)

Print (“Date=”, timestamp)

Output:-

Date=2012-01-1

Print year, month, hour, minute and timestamp

Example:-

From datetime import datetime

A=datetime (2020, 11, 28, 23, 55, 59, 342380)

Print(“year=”, a.year) Print(“year=”, a. month)

Print(“year=”, a.hour) Print(“year=”, a.minute)

Print(“year=”, a.timestamp ())

Output:-

Year=2020

Month=11

Day=28

Hour=23

Minute=55

Timestamp=1511913359.34238

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India