Import datetime
Import time
#获取当前时间
Datetime.datetime.now ()
#获取当前日期
Datetime.date.today ()
#字符串转换为时间格式
>>> t = time.strptime ("2009-08-08", "%y-%m-%d")
>>> y,m,d = T[0:3]
>>> datetime.date (y,m,d)
Datetime.date (2009, 8, 8)
>>> Datetime.datetime (y,m,d)
Datetime.datetime (2009, 8, 8, 0, 0)
#返回前一天的日期
>>> Import datetime
>>> today = Datetime.date.today ()
>>> days = Datetime.timedelta (Days=1)
>>> Print Today
2010-04-15
>>> Print Days
1 day, 0:00:00
>>> Print Today-days
2010-04-14
>>>
#datetime object gets the date of the day
>>> now = Datetime.datetime.now ()
>>> today = Now.date (). Today ()
>>> Print Today
2010-04-15
>>> Print Now
2010-04-15 14:41:21.421000
Get time stamp
>>> Time.time ()
1446885023.80355
Time Stamp Conversion time
>>> time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ()))
' 2015-11-07 16:32:45 '
Time-conversion Timestamp
>>> time.mktime (Time.strptime (' 2015-11-06 16:30:00 ', '%y-%m-%d%h:%m:%s '))
1446798600.0
Get year and day time and seconds
>>> time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ())
' 2015-11-07 16:45:46 '
>>> Print (Time.strftime ("%y-%m-%d%x", Time.localtime ()))
2015-11-07 16:52:31
Get subtle (one out of 10,000 seconds)
>>> Datetime.datetime.now (). microsecond
327545
Gets the time + number of microseconds
>>> now = Datetime.datetime.now ()
>>> "%s.%s"% (now.strftime ('%y%m%d%h%m%s '), Now.microsecond)
' 20151107172412.390765 '
Python formats a datetime function as Datetime.datetime.strftime (), a function that converts a string into a date type: Datetime.datetime.strptime (), and two functions that involve a DateTime formatted string. Listed below:
%a abbreviated weekday name
%A Full weekday name
%b Abbreviated month name
%B Full month name
%c Date and time representation appropriate for locale
%d day of month as decimal number (01-31)
%H Hour in 24-hour format (00-23)
%I Hour in 12-hour format (01-12)
%j Day of the Year as decimal number (001-366)
%m Month as decimal number (01-12)
%M Minute as decimal number (00-59)
%p current locale ' s a.m./p.m. indicator for 12-hour clock
%s Second as decimal number (00-59)
%u Week of year as decimal number, with Sunday as first day of Week (00-51)
%w Weekday as decimal number (0-6; Sunday is 0)
%W Week of year as decimal number, with Monday as first day of Week (00-51)
%x Date representation for current locale
%x time representation for current locale
%y year without century, as decimal number (00-99)
%Y year with century, as decimal number
%z,%z time-zone name or abbreviation; No characters if time zone is unknown
Percent Percent Sign
Example:
String converted to DateTime
>>> C = datetime.datetime.strptime (' sep-21-09 16:34 ', '%b-%d-%y%h:%m ');
>>> C
Datetime.datetime (2009, 9, 21, 16, 34)
DateTime converted to String
>>> Datetime.datetime.now (). Strftime ('%b-%d-%y%h:%m:%s ');
' Sep-22-09 16:48:08 '
Python Time operations