Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Python – Get Current Date and Time – Examples & Explanation

 

Python datetime class can be used to get and manipulate date and time. today() method of datetime class can be used to get the current date and time in programs. Python datetime class is flexible and provides a lot of methods and properties to express the date and time in programs in a variety of formats effectively. Let’s discuss Python datetime class with examples and explanations.

 

Python date and time image

Python datetime with examples & explanation

 

You can format date and time in various ways using built-in date time format specifiers or directives in Python. Python has quite a lot built-in date and time format specifiers or directives available. There is a detail list and various examples at the bottom of this article.

Getting Current Date and Time in Python using datetime.today() Method


Output:

Current date and time: 2017-01-09 03:47:21.595913
Current year: 2017
Current month: 5
Current day: 9
Current hour: 3
Current minutes: 47
Current seconds: 21
Current micro seconds: 595913

 

How to Format Current Date in Python using strftime Function

You can use strftime method to format a date in Python. You can convert a date into any possible desired format by using this method. strftime method is used to create a string representing the date under the control of explicit format string.

You can use format specifiers or directives mentioned in the table “Complete List of Python DateTime Format Specifiers” below in this article to create explicit date format string.

In the following example, you can use % (character) directive to format date according to your requirement. Look at the following example carefully.

Output:

Current date and time: 2017-01-09 04:08:02.214873
09-01-2017
09-January-2017 04:08:02

By using directives from the following table you can create a format of your date string.

 

Complete List of Python Date and Time Format Specifiers or Directives

By using directives from the following table you can create the format of your date string.

%a – Locale’s abbreviated weekday name.

%A – Locale’s full weekday name.

%b – Locale’s abbreviated month name.

%B – Locale’s full month name.

%c – Locale’s appropriate date and time representation.

%d – Day of the month as a decimal number [01,31].

%H – Hour (24-hour clock) as a decimal number [00,23].

%I – Hour (12-hour clock) as a decimal number [01,12].

%j – Day of the year as a decimal number [001,366].

%m – Month as a decimal number [01,12].

%M – Minute as a decimal number [00,59].

%p – Locale’s equivalent of either AM or PM.

%S – Second as a decimal number [00,61].

%U – Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.

%w – Weekday as a decimal number [0(Sunday),6].

%W – Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.

%x – Locale’s appropriate date representation.

%X – Locale’s appropriate time representation.

%y – Year without century as a decimal number [00,99].

%Y – Year with century as a decimal number.

%Z – Time zone name (no characters if no time zone exists).

%% – A literal "%" character.

 

How to Get Current Date and Time using ISO Format in Python


Output:

Current date in ISO format: 2017-01-09T04:14:58.947708

Loading...
LearnAndLearn.com © 2018 - All rights reserved