Python time module-time and datetime

Source: Internet
Author: User
Tags month name string format timedelta

Time Module

The time representation in Python is: timestamp, which is the number of seconds from January 1, 1975 00:00:00 to now, the time string after formatting, and the time struct_time tuple.
Elements in the Struct_time tuple mainly include tm_year (year), Tm_mon (month), Tm_mday (Day), Tm_hour (Time), Tm_min (min), tm_sec (seconds), Tm_wday (weekday0-6 (0 for Sunday)), Tm_yday (Day 1-366 of the year), TM_ISDST (Daylight saving Time)

1. Time
  • Common functions

      • Time.time () returns the current timestamp
    >>> time.time()1465370844.096474
      • Time.ctime () returns the time character of this format ' Wed June 8 15:27:48 2016 ', showing the current time. You can also convert timestamps
    >>> time.ctime()'Wed Jun  8 15:27:48 2016'>>> time.ctime(time.time()-86400)'Tue Jun  7 15:29:36 2016'
      • Time.gmtime converts the timestamp to the Struct_time format, which shows the time in Greenwich 0 time zone
    >>> time.gmtime()time.struct_time(tm_year=2016, tm_mon=6, tm_mday=8, tm_hour=7, tm_min=34, tm_sec=28, tm_wday=2, tm_yday=160, tm_isdst=0)>>> time.gmtime(time.time() - 86400)time.struct_time(tm_year=2016, tm_mon=6, tm_mday=7, tm_hour=7, tm_min=34, tm_sec=41, tm_wday=1, tm_yday=159, tm_isdst=0)
      • Time.localtime converting the current system timestamp to struct_time format
    >>> time.localtime()time.struct_time(tm_year=2016, tm_mon=6, tm_mday=8, tm_hour=15, tm_min=35, tm_sec=33, tm_wday=2, tm_yday=160, tm_isdst=0)>>> time.localtime(time.time() - 86400)time.struct_time(tm_year=2016, tm_mon=6, tm_mday=7, tm_hour=15, tm_min=37, tm_sec=10, tm_wday=1, tm_yday=159, tm_isdst=0)
      • Time.mktime struct_time format back to timestamp
    >>> now = time.localtime()>>> nowtime.struct_time(tm_year=2016, tm_mon=6, tm_mday=8, tm_hour=15, tm_min=38, tm_sec=28, tm_wday=2, tm_yday=160, tm_isdst=0)>>> time.mktime(now)1465371508.0
      • Time.strftime convert the Struct_time format to the specified string format
    >>> now = time.localtime()>>> nowtime.struct_time(tm_year=2016, tm_mon=6, tm_mday=8, tm_hour=15, tm_min=38, tm_sec=28, tm_wday=2, tm_yday=160, tm_isdst=0)>>> last = time.localtime(time.time() - 86400)>>> lasttime.struct_time(tm_year=2016, tm_mon=6, tm_mday=7, tm_hour=15, tm_min=40, tm_sec=23, tm_wday=1, tm_yday=159, tm_isdst=0)>>> time.strftime("%Y-%m-%d %H:%M:%S",last)'2016-06-07 15:40:23'>>> time.strftime("%Y-%m-%d %H:%M:%S",now)'2016-06-08 15:38:28'
      • Time.strptime Convert a custom time format string to struct_time format
    >>> time.strptime("2016-06-08","%Y-%m-%d")time.struct_time(tm_year=2016, tm_mon=6, tm_mday=8, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=160, tm_isdst=-1)>>> time.strptime("2016-06-08 15:50:44","%Y-%m-%d %H:%M:%S")time.struct_time(tm_year=2016, tm_mon=6, tm_mday=8, tm_hour=15, tm_min=50, tm_sec=44, tm_wday=2, tm_yday=160, tm_isdst=-1)
      • Time.sleep pause time, similar to the Shell's sleep ()
  • Other

      • Time Format:
    format Description
    %a Show Simplified Week name
    %A Show Full week name
    %b Show Simplified month name
    %B Show full month name
    %c Local corresponding date and time representation
    %d Show the day of the month
    %H Display hours in 24-hour system
    %I Display hours in 12-hour system
    %j Show the day of the year
    %m Show month
    %M Number of minutes displayed)
    %p Local AM or PM's corresponding character
    %s Number of seconds to display)
    %u The number of weeks in a year
    %w Displays the day of the week, the default starting from 0 for Monday
    %W Basically the same as%u
    %x Local corresponding date
    %x Local corresponding time
    %y The year of the century was removed (00-99)
    %Y The full year
    %Z The name of the time zone (if it does not exist as a null character)
    %% '% ' character
      • Converting relationships between Time functions

    ?

2. DateTime

Datime is an upgraded version of time, and can be managed separately for three types of date (date), Time, DateTime, and date time. Mainly consists of the following four classes

    • Datetime.datetime Common functions (datetime.date datetime.time Universal)
    #datetime.datetime.today () default returns the current date and time object, or you can customize the date and time >>> today = Datetime.datetime.today () >>&gt ; Print (today) 2016-06-08 16:34:08.163371 >>> last = Datetime.datetime (2016,5,8,11,23,55) # Note here that the date is only the actual month, not can take 0 >>> print (last) 2016-05-08 11:23:55 >>>last = Datetime.datetime (2016,05,08,11,23,55) F Ile "<stdin>", line 1 last = Datetime.datetime (2016,05,08,11,23,55) ^syntaxerro R:invalid Token#datetime.datetime.now () Back to current time >>> Datetime.datetime.now () datetime.datetime (2016, 6, 8, 16, 694398) # datetime.strftime (format) # Custom format time >>> today.strftime ("%i:%m:%s%p%d/%m/%y") ' 0 4:34:08 PM 08/06/2016 ' # datetime.datetime.timple () convert time to Struct_time format >>>today.timetuple () Time.struct_ti Me (tm_year=2016, tm_mon=6, Tm_mday=8, tm_hour=16, tm_min=34, Tm_sec=8, tm_wday=2, tm_yday=160, Tm_isdst=-1) #datetime.replace () returns a replaced Date object >>&Gt;last = Today.replace (1949,10,1) >>> print (last) 1949-10-01 16:34:08.163371>>> last = Today.replace  (year=1919,month=3,day=2) >>> print (last) 1919-03-02 16:34:08.163371# datetime.datetime.strptime Convert string to log Format object >>> a = "2016-06-08 17:18:19" >>> B = datetime.datetime.strptime (A, "%y-%m-%d%h:%m:%s") >>> print (a) 2016-06-08 17:18:19>>> print (b) 2016-06-08 17:18:19>>> print (Type (a)) < Class ' str ' >>>> print (type (b)) <class ' Datetime.datetime ' >
    • Datetime.timedelta Time Arithmetic

Available parameters: Days seconds microseconds milliseconds minutes hours weeks

today = datetime.datetime.now()>>> print(today)2016-06-08 16:51:31.698122>>> yesterday = today - datetime.timedelta(days=1)>>> print(yesterday)2016-06-07 16:51:31.698122>>> last_hour = today - datetime.timedelta(hours=1)>>> print(last_hour)2016-06-08 15:51:31.698122

Python time module-time and datetime

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.