Get the format of the current date in Python

Source: Internet
Author: User
Tags month name
How do I get the current date and time in Python? In the Python language, what module or class function can we call to get the current time or date?

Of course you can use the time module, which provides a variety of time-related functions. But some of the functions in this module are not available in some platforms. So what do we do? We
You can use a more advanced object-oriented interface function: DateTime. It provides a variety of simple or complex ways to manipulate dates and times.

Use the time module in Python to get the current

Time.strftime (format) time.strftime ("%h:%m:%s") # #24小时格式time. Strftime ("%i:%m:%s") # # 12-hour format

Example
A simple Python program that gets the date and time of the day

#!/usr/bin/pythonimport Timeprint (Time.strftime ("%h:%m:%s")) # # hour format # #print (Time.strftime ("%i:%m:%s"))

Example output:

18:11:306:11:30

A python program that prints out the current date

#!/usr/bin/pythonimport time## dd/mm/yyyy Format print (Time.strftime ("%d/%m/%y"))

Example output:
11/10/2013

Format parameters:

%a Day of the week%a the full name of the day of the week%b month of the abbreviated%b month full name%c standard date of the time string%c year of the last two digits%d decimal representation of the month of the first few%d Month/day/year%e in two-character fields, decimal represents the number of days of the month%f year-month-day%g year of the last two digits, using the week-based year%g years, using the month name based on the week%h abbreviation%h 24-Hour Hour%i The 12-hour hour%j decimal indicates the number of minutes of the day of the year%m decimal representation of the month%m the minute of the%n New line character%p local AM or PM equivalent display%r 12 hour time%r display hours and minutes: hh:mm%s decimal seconds%t Horizontal tab%t Display hours/minute: hh:mm:ss%u days of the week, Monday for the first day (value from 0 to 6, Monday is 0)%u the week of the year, the Sunday as the first day (value from 0 to)%V the week of the year, using a week-based year%w decimal Day (value from 0 to 6, Sunday is 0)% In the first week of the year, the time string of Monday as the date string%x standard (value from 0 to the range)%x standard is%Y the decimal year without the Century (values from 0 to%Y) The 10 year%z,%z time zone name with the century part, and a null character if the time zone name cannot be obtained. Percent hundred percent semicolon

Use the DateTime module to get the current date and time
The parameters are as follows:

Cur=datetime.datetime.now () Cur.hourcur.minutecur.yearcur.daycur.month

Example:

#!/usr/bin/pythonimport Datetimei = Datetime.datetime.now () print ("The current date and time is%s"% i) print ("Date and time in ISO format is%s"% I.isoform at ()) print ("Current year is%s"%i.year) print ("Current month is%s"%i.month) print ("Current date is  %s"%i.day) print ("dd/mm/yyyy format is  %s/ %s/%s "% (I.day, I.month, i.year)) print (" Current hour is%s "%i.hour) print (" Current minute is%s "%i.minute) print (" Current seconds is  %s "%i.second)

Example output:

Current date and time = 2013-10-11 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 c4/>11/10/2013 Current hour is 0 the current minute is 38 current seconds 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.