Obtain the format of the current date in Python, and obtain the current date in python.
How can I get the current date and time in Python? In Python, what modules or class functions can we call to obtain the current time or date?
Of course, you can use the time module, which provides various time-related functions. However, some functions in this module are not available on some platforms. So what should we do? We
You can use a more advanced object-oriented interface function: datetime. It provides a variety of simple or complex methods to operate on dates and times.
Use the time module in python to obtain the current time
1 2 3
|
Time. strftime (format) Time. strftime ("% H: % M: % S ")#24-hour format Time. strftime ("% I: % M: % S ")#12-hour format |
Example
A simple python program for obtaining the date and time of the current day
1 2 3 4 5 6 7
|
#! /Usr/bin/python
ImportTime Print(Time. strftime ("% H: % M: % S "))
#12 hour format ## Print(Time. strftime ("% I: % M: % S ")) |
Sample output:
Python program that prints the current date
1 2 3 4 5
|
#! /Usr/bin/python
ImportTime # Dd/mm/yyyy format Print(Time. strftime ("% d/% m/% Y ")) |
Sample output:
11/10/2013
Format parameters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
% A abbreviation of the day of the week % A full name of the day of the week Abbreviation of % B month % B full name % C standard date time string The last two digits of the Year % C % D indicates the day of the month in decimal format. % D month/day/year % E indicates the day of the month in decimal format in the two-character field % F-month-day The last two digits of the Year % g. The year is based on the week. % G year, based on the week Year % H abbreviated month name % H in 24-hour format % I 12-hour % J indicates the day of the year in decimal format. Month in % m decimal format % M decimal number of minutes % N new line operator % P equivalent display of local AM or PM % R 12 hours % R display hour and minute: hh: mm % S decimal seconds % T horizontal Tab % T display time in seconds: hh: mm: ss % U the day of the week, Monday is the first day (value ranges from 0 to 6, Monday is 0) % U indicates the week of the year. Sunday is regarded as the first day (the value ranges from 0 to 53) % V indicates the week of the year, which is based on the year of the week. % W decimal indicates the day of the week (the value ranges from 0 to 6, and Sunday is 0) % W indicates the week of the year. Monday is the first day (from 0 to 53) % X standard date string % X standard time string % Y does not contain the century's decimal year (the value ranges from 0 to 99) % Y indicates the tenth year of the century. % Z, % Z Time Zone name. If the time zone name cannot be obtained, an empty character is returned. % Percent sign |
Use the datetime module to obtain the current date and time
The parameters are as follows:
1 2 3 4 5 6
|
Cur = datetime. datetime. now () Cur. hour Cur. minute Cur. year Cur. day Cur. month |
Example:
1 2 3 4 5 6 7 8 9 10 11 12
|
#! /Usr/bin/python ImportDatetime I = datetime. datetime. now () Print("The current date and time are % s" % I) Print("ISO format date and time is % s" % I. isoformat ()) Print("The current year is % s" % I. year) Print("The current month is % s" % I. month) Print("The current date is % s" % I. day) Print("Dd/mm/yyyy format is % s/% s" % (I. day, I. month, I. year )) Print("Current hour is % s" % I. hour) Print("The current minute is % s" % I. minute) Print("Current second is % s" % I. second) |
Sample output:
1 2 3 4 5 6 7 8 9
|
Current date and time = 19:38:19. 4545 ISO format date and time = 2013-10-11T19: 38: 19.4545 Current year 2013 Current month 10 Current date 11 Dd/mm/yyyy format: 11/10/2013 The current hour is 0 The current minute is 38 The current second is 19 |