Python Datetime

Python Datetime

In python datetime is a module. We get the date, month, year, day etc from datetime module.

See Output

Get the current date and time

The now() is a class method of the datetime class. It returns the current local date and time.

See Output

Get the current date and time of a specific timezone

The now(tz=None) method uses tz argument. The tz argument is optional argument of now(tz=None) method.

See Output

Format of datetime object into readable strings

The directive is a special code like %a, %A, %w etc in python datetime module. The strftime(format) method used to represent date and time string in readable format.

%a : It is used to represent weekday in short version. Example: Mon
%A : It is used to represent weekday in full version. Example: Monday
%w : It is used to represent weekday as number. 0=Sunday, 1= Monday, 2=Tuesday, 3=Wednesday, 4=Thursday,5=Friday, 6=Saturday. Example: 5
%d : It is used to represent day of month 01-31. Example: 29
%b : It is used to represent month name in short version. Example: Oct
%B : It is used to represent month name in full version. Example: October
%m : It is used to represent month number 01-12. Example:10
%y : It is used to represent year in short version like 25.
%Y : It is used to represent year in full version like 2025.
%H : It is used to represent hour 00-23. Example:18
%I : It is used to represent hour 00-12. Example:09
%p : It is used to represent AM/PM. Example:AM
%P : It is used to represent am/pm. Example:am
%M : It is used to represent minute 00-59. Example:50
%S : It is used to represent second 00-59. Example:35

%f : It is used to represent microsecond 000000-999999. Example:523745
%Z : It is used to represent timezone. Example: IST
%z: It is used to represent the UTC offset in the form +HHMM or -HHMM. Example: +0530

See Output

Create the format of date and time

See Output

Create datetime object from a date and time string

The strptime(datetime_string, format) method used to create datetime object from date and time string. The strptime(datetime_string, format) method takes two arguments.

datetime_string: The datetime_string contains date and/or time string that needs to be converted in object.

format: Write the format for datetime_string that match same structure of datetime_string.

See Output

About Us

Comsysapp.com is an educational website. Students and software developers can learn programming language tutorials. Comsysapp is very useful for beginners and professional developers. Comsysapp provides tutorial in easy language. Comsysapp.com has focus on simplicity.

Services

Comsysapp.com provides free tutorials like c, html, css, etc. All tutorials are free for beginner and professionals.


Terms of Use

comsysapp.com is not responsible for any mistake. We are not responsible if information made available on our website is incomplete or invalid. But comsysapp.com always try for zero-zero mistake.

Privacy policy

comsysapp.com does not collect any data from users. We use Google AdSense advertising on our website. We never ask personal or private information.


Sitemap

sitemap

copyright © 2023