Python time and datetime

time module

Interval is in seconds of floating point decimals.

Each time stamps are to midnight since January 1970 1 (epoch) after how long to represent.

There are many common functions can be converted to date format in Python's time module. The function of the time.time () to get the current time stamp, the following examples:

Import time;   # introduction time module 
 
ticks = the time.time ()
 Print ( " current time stamp as: " , ticks) 

the current timestamp: 1569829768.129234

Time tuple (struct_time tuple)

No. Field

Attributes

value
0 4-Digit Year tm_year 2008
1 month tm_mon 1-12
2 day tm_mday 1-31
3 hour tm_hour 0-23
4 minute tm_min 0-59
5 second tm_sec 0-61 (60 or 61 leap seconds)
6 The first few days of the week tm_wday 0-6 (0 is Monday)
7 The first few days of the year tm_yday 1-366 (Julian calendar)
8 summer time tm_isdst -1, 0, 1, -1 is determined whether the flag when daylight saving time

 

 

 

 

 

 

 

 

 

 

Get the current time

Import Time 
 
localtime = time.localtime (the time.time ())
 Print ( " Local time is: " , localtime) 

local time: time.struct_time (tm_year = 2019, tm_mon = 9, tm_mday = 30, tm_hour = 15, tm_min = 49, tm_sec = 53, tm_wday = 0, tm_yday = 273, tm_isdst = 0)

Get formatted time

Import Time 
 
localtime = that time.asctime (time.localtime (the time.time ()))
 Print ( " Local time is: " , localtime) 

local time: Mon Sep 30 15:50:12 2019

Date Format

# 2019-09-30 15:59:18 formatted form 
Print (The time.strftime ( "% D%% Y-M-% H:% M:% S", time.localtime ()))

# formatted Mon Sep 30 15:59:18 2019 form
Print (the time.strftime ( "% A% B% D% H:% M:% S% the Y", time.localtime ()))

# format string to a timestamp
= A "Mon On Sep 30 15:59:18 2019"
Print (time.mktime (the time.strptime (A, "% A% B% D% H:% M:% S% the Y")))

2019-09-30 16:00:01
Mon Sep 30 16:00:01 2019
1569830358.0

python, date and time format symbols:

  • % Y represents two-digit year (00-99)
  • % Y represents a four-digit year (000-9999)
  • % M (01-12)
  • Within a% d day of the month (0-31)
  • % H 24 hours (0-23) manufactured by h
  • % I 12 hours hour (01-12)
  • % M number of minutes (00 = 59)
  • % S seconds (00-59)
  • % A week simplify local name
  • % A full weekday name local
  • % B local simplify month name
  • % B Full month name of the local
  • % C represents the corresponding date and time represent the local
  • One day (001-366)% j years
  • % P local AM or PM equivalent character
  • % U week number of the year (00-53) for the week beginning Sunday
  • % W week (0-6), Sunday is the start of week
  • % W week number of the year (00-53) for the week beginning Monday
  • % X indicates the corresponding local date
  • % X indicates the corresponding local time
  • Name% z current time zone
  • %%% Number itself

The method of time module

  • time.localtime ([secs]): The time stamp is converted to a current time zone struct_time. Secs If the parameter is not provided, the current time places subject.

  • time.gmtime ([secs]): and the like localtime () method, gmtime () method is to convert a timestamp struct_time the UTC time zone (0:00 region).

  • time.time (): Returns the timestamp of the current time.

  • time.mktime (t): The conversion of a struct_time timestamp.

  • time.sleep (secs): thread running postpone the specified time, in seconds.

  • time.asctime ([t]): the tuple indicates a time or as expressed in this form struct_time: 'Sun Oct 1 12:04:38 2019'. If there are no parameters, it will be time.localtime () as a parameter.

  • time.ctime ([secs]): to a timestamp (in seconds calculated in floating point) is converted to that time.asctime () form. If the parameter is not given time or None, it will default time.time () as a parameter. It acts time.asctime (time.localtime (secs)).

  • time.strftime (format [, t]): representing time to a tuple or struct_time (as returned by time.localtime () and time.gmtime ()) is converted to time format string. If t is not specified, the incoming time.localtime ().

  • Example: time.strftime ( "% Y-% m-% d% X", time.localtime ()) # outputs "2017-10-01 12:14:23"

  • time.strptime (string [, format]): the time a formatted string into struct_time. In fact it strftime () is the inverse operation.

    • 举例:time.strptime(‘2017-10-3 17:54’,”%Y-%m-%d %H:%M”) #输出 time.struct_time(tm_year=2017, tm_mon=10, tm_mday=3, tm_hour=17, tm_min=54, tm_sec=0, tm_wday=1, tm_yday=276, tm_isdst=-1)

Time conversion relationship

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

datetime module

Several methods datetime function

  • datetime.date: represents the class date. Common attributes are year, month, day;

  • datetime.time: represents the class time. Common attributes are hour, minute, second, microsecond;

  • datetime.datetime: indicate the date and time.

  • datetime.timedelta: indicates the time interval, i.e. the length of time between the two points.

  • datetime.tzinfo: information related to the time zone. (Not detailed here fully discussed in the class, interested python shoes can refer to the manual)

Print (datetime.datetime.now ())   # Returns the current datetime date type 
Print (datetime.date.fromtimestamp (99999999))   # put into a timestamp datetime date type 
Print (datetime.datetime.now () + datetime. timedelta (. 4))   # current time day +4 
Print (datetime.datetime.now () + the datetime.timedelta (=. 4 hours))   # current time +4 hours 
Print (datetime.datetime.now (). Replace (year = 2999, month The = 11, Day 30 =))   # time to replace

 
2019-09-3021: 00: 33.774001 
1973-03-03 
2019-10-0421: 00: 33.774001 
2019-10-0101: 00: 33.774001 
2999 -11-3021: 00: 33.774001

 

Guess you like

Origin www.cnblogs.com/bt14/p/11614158.html
Recommended