Processing of time in Python-datetime

Source: Internet
Author: User
#  ! /Usr/bin/Python  
# Coding = UTF-8

Import Datetime

"""
Powerful functions of datetime
Supported from 0001 to 9999
"""

"""
Current Time
Returns a datetime type.
The now method has a parameter TZ to set the time zone type. If the effect is not the same as that of the today method
"""
Now = datetime. datetime. Now ()
# UTC time
Datetime. datetime. utcnow ()
Attrs = [
( " Year " , " Year " ),( ' Month ' , " Month " ),(" Day " , " Day " ),( ' Hour ' , " Hours " ),( ' Minute ' , " Minute " ),( ' Second ' , " Seconds " ),( ' Microsecond ' , " Millisecond " ),(
' Min ' ," Minimum " ),( ' Max ' , " Max " ),
]
For K, V In Attrs:
" Now. % s = % s # % s " % (K, getattr (now, k), V)


"""
Returns a time structure.
"""
Now. timetuple ()

"""
Returns a date type.
Http://www.cnblogs.com/goodspeed/archive/2011/11/07/python_date_time.html
"""
Now. Date ()

"""
Returns a time type.
Http://www.cnblogs.com/goodspeed/archive/2011/11/07/python_date_time.html
"""
Now. Time ()

"""
The current day of the week. Monday is 0, and the week is 6
Note that the method is not an attribute.
"""
Now. weekday ()

"""
The current day of the week. Monday is 1, week is 7
Note that the method is not an attribute.
"""
Now. isoweekday ()

"""
Modify the current time. For example, change it to the 1st day of the current month.
"""
Now. Replace (Day = 1)

Past = datetime. datetime (2010,11, 12,13, 14,15, 16)

"""
Comparison
The returned value is of the timedelta type.
Http://www.cnblogs.com/goodspeed/archive/2011/11/06/python_timedelta.html
"""
Now-past
"""
Convert to string
For detailed rules, see time
Http://www.cnblogs.com/goodspeed/archive/2011/11/06/python_time.html
"""
Strdatetime = now. strftime ( " % Y-% m-% d % H: % m: % s " )
"""
String generation datetime object
"""
Datetime. datetime. strptime (strdatetime, " % Y-% m-% d % H: % m: % s " )
Related Article

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.