Obtain the format of the current date in Python

Source: Internet
Author: User
Tags month name time zone names
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? 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

Time. strftime (format) time. strftime ("% H: % M: % S") #24-hour format time. strftime ("% I: % M: % S") #12-hour format

A simple python program for obtaining the date and time of the current day

#!/usr/bin/pythonimport timeprint (time.strftime("%H:%M:%S"))## 12 hour format ##print (time.strftime("%I:%M:%S"))

Sample output:


Python program that prints the current date

#! /Usr/bin/pythonimport time # dd/mm/yyyy format print (time. strftime ("% d/% m/% Y "))

Sample output:

Format parameters:

% A abbreviation of the day of the week % A the full name of the day of the week % B the short abbreviation of the Month % B % c standard date time string % C the last two digits of the year % d decimal the day % D month/day/year % e of the month is in the two-character domain, the last two digits of the day % F year-month-day % g year of the month in decimal format, which is based on the year % G year of the week, the day of each year % m decimal month in the hour % I 12 hour format of the month name % h 24 hour format % M decimal representation of the number of minutes % n newline operator % p local AM or PM equivalent display % r 12 hours time % R display hours and minutes: hh: mm % S decimal seconds % t horizontal tab % T display hour minute second: hh: mm: ss % u day of the week, Monday is the first day (value from 0 to 6, monday is 0) % U the week of the year, and Sunday is used as the week of the year for the first day (value ranges from 0 to 53) % V, week-based year % w decimal representation of the day of the week (value from 0 to 6, Sunday is 0) % W the number of the year Week, Monday as the first day (value from 0 to 53) % x standard date string % X standard time string % y without Century decimal year (value from 0 to 99) % Y indicates the tenth year % z and % Z time zone names of century. 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:



#! /Usr/bin/pythonimport datetimei = datetime. datetime. now () print ("current date and time is % s" % I) print ("ISO format date and time is % s" % I. isoformat () print ("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 ("current minute is % s" % I. minute) print ("current second is % s" % I. second)

Sample output:

Current date and time = 19:38:19. 4545ISO format date and time = 2013-10-11T19: 38: 19.4545 current year 2013 Current month 10 current date 11dd/mm/yyyy format is 11/10/2013 current hour is 0 current minute is 38 current second is 19

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.