Date and Time module

Source: Internet
Author: User
Tags local time month name

Excerpt from: http://my.oschina.net/935572630/blog/397557

Http://www.runoob.com/python/python-date-time.html

Date class

The date class represents a day. The date is made up of the year, Month and day (the Earth knows ~ ~). The constructor for the date class is as follows:

Class Datetime.date (year, Month, day):

Date.today (): Returns a Date object that represents the current local date;

# Date.today (): 2010-04-06

Date.weekday (): Returns weekday, if it is Monday, returns 0 if it is Week 2, returns 1, and so on;

Data.isoweekday (): Returns weekday, if it is Monday, returns 1 if it is Week 2, returns 2, and so on;

Date.strftime (FMT): Custom formatted string. Explained in detail below.

Time class

The time class represents times, consisting of hours, minutes, seconds, and microseconds. (I'm not from Mars.) The constructor for the time class is as follows:

Class Datetime.time (hour[, minute[, second[, microsecond[, Tzinfo]]): The meaning of each parameter is not explained, note here the parameter tzinfo, which represents the time zone information. Note the range of values for each parameter: The range of Hour is [0], the range of minute is [0], the range of second is [0, 0], and the range of microsecond is [1000000].

class properties defined by the time class:

    • The minimum and maximum time that the Time.min, Time.max:time class can represent. wherein, Time.min = time (0, 0, 0, 0), Time.max = time (23, 59, 59, 999999);

    • Time.resolution: The smallest unit of time, here is 1 microseconds;

The time class provides instance methods and properties:

    • Time.hour, Time.minute, Time.second, Time.microsecond: Hours, minutes, seconds, microseconds;

    • Time.tzinfo: time zone information;

    • Time.replace ([hour[, minute[, second[, microsecond[, Tzinfo]]]]): Creates a new time object with the time, minutes, seconds, and microseconds specified by the parameter instead of the attributes in the original object (the original object remains unchanged);

    • Time.isoformat (): A string representation of the return type, such as "HH:MM:SS" format;

    • Time.strftime (FMT): Returns a custom formatted string. described in detail below;

DateTime class

DateTime is a combination of date and time, including all information about date and time. Its constructor is as follows: Datetime.datetime (year, month, day[, hour[, minute[, second[, microsecond[, Tzinfo] []]), the meaning of each parameter with date, As in the constructor of time, be aware of the range of parameter values.

The class properties and methods defined by the DateTime class:

Datetime.today (): Returns a DateTime object representing the current local time;

# Today (): 2010-04-07 09:48:16.234000

DateTime.Now ([TZ]): Returns a DateTime object that represents the current local time, and if the parameter tz is provided, gets the local time of the time zone referred to by the TZ parameter;

Datetime.strptime (date_string, format): Converts a format string to a DateTime object

    • Datetime.year, month, day, hour, minute, second, microsecond, Tzinfo:

    • Datetime.date (): Gets the Date object;

    • Datetime.time (): Gets the time object;

format string

datetime, date, and time all provide the strftime () method, which receives a format string that outputs a string representation of the DateTime.

Python format symbols in time Date:

    • %y Two-digit year representation (00-99)
    • %Y Four-digit year representation (000-9999)
    • %m Month (01-12)
    • One day in%d months (0-31)
    • %H 24-hour hours (0-23)
    • %I 12-hour hours (01-12)
    • %M minutes (00=59)
    • %s seconds (00-59)
    • %a Local Simplified Week name
    • %A Local Full week name
    • %b a locally simplified month name
    • %B Local Full month name
    • %c Local corresponding date representation and time representation
    • %j Day of the Year (001-366)
    • %p the equivalent of a local a.m. or p.m.
    • %u weeks of the year (00-53) Sunday is the beginning of the week
    • %w Week (0-6), Sunday for the beginning of the week
    • %W Week of the Year (00-53) Monday is the beginning of the week
    • %x Local corresponding date representation
    • %x Local corresponding time representation
    • %Z the name of the current time zone
    • Percent% of the number itself

Example:

[python] view plaincopy

    1. DT = DateTime.Now ()

    2. print ' (%y-%m-%d %h:%m:%s %f):  ' ,  Dt.strftime (%y-%m-%d %h:%m:%s % F '

    3. print ' (%y-%m-%d %h:%m:%s %p):  ' ,  Dt.strftime (%y-%m-%d %i:%m:%s % P '

    4. print '%%a: %s  ' '%a '

    5. print '%%a: %s  ' '%A '

    6. print '%%b: %s  ' '%b '

    7. print '%%b: %s  ' '%B '

    8. print ' date Time%%c: %s  ' '%c '

    9. print ' date%%x:%s  '

    10. print ' time%%x:%s  ' '%x '

    11. print ' Today is this week's%s day   '

    12. print ' Today is this year's%s day   '

    13. Print ' This week is this year's%s week ' % dt.strftime ('%u ')

    14. # #----Results----

    15. # (%y-%m-%d%h:%m:%s%f): 2010-04-07 10:52:18 937000

    16. # (%y-%m-%d%h:%m:%s%p): 10-04-07 10:52:18 AM

    17. #%a:wed

    18. #%a:wednesday

    19. #%B:APR

    20. #%b:april

    21. # Date Time%C:04/07/10 10:52:18

    22. # date%X:04/07/10

    23. # time%x:10:52:18

    24. # today is the 3rd day of the Week #

    25. # today is the No. 097 day of the Year #

    26. # This week is the 14th week of this year

Date and Time module

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.