Python-time, Datetimme module

Source: Internet
Author: User
Tags month name timedelta

Time Module 1.time.time (): Returns the timestamp of the current time.

Print time stamp:

>>>import time>>> time.time()1530329387.173301
2.time.localtime ([secs]): Converts a timestamp to the struct_time of the current time zone. The secs parameter is not provided, whichever is the current time.

Current time:

>>> time.localtime()time.struct_time(tm_year=2018, tm_mon=6, tm_mday=30, tm_hour=11, tm_min=31, tm_sec=56, tm_wday=5, tm_yday=181, tm_isdst=0)  

You can splice yourself:

>>> t = time.localtime()>>> ‘%s-%s-%s‘%(t.tm_year,t.tm_mon,t.tm_mday)‘2018-6-30‘>>>

Incoming timestamp parameter, return to normal time

>>> time.localtime(123142312)time.struct_time(tm_year=1973, tm_mon=11, tm_mday=26, tm_hour=14, tm_min=11, tm_sec=52, tm_wday=0, tm_yday=330, tm_isdst=0)>>>
int tm_sec; /* 秒 – 取值区间为[0,59] */int tm_min; /* 分 - 取值区间为[0,59] */int tm_hour; /* 时 - 取值区间为[0,23] */int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */int tm_year; /* 年份,其值等于实际年份减去1900 */int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的时候,tm_isdst为0;不了解情况时,tm_isdst()为负。
Time.gmtime ([secs]): Similar to the LocalTime () method, the Gmtime () method is the struct_time of converting a timestamp to the UTC time zone (0 o'clock Zone).

Time difference 8 hours

3.time.mktime (t): Converts a struct_time to a timestamp.
>>>= time.localtime()>>> atime.struct_time(tm_year=2018, tm_mon=6, tm_mday=30, tm_hour=11, tm_min=43, tm_sec=56, tm_wday=5, tm_yday=181, tm_isdst=0)>>> time.mktime(a)1530330236.0
4.time.sleep (secs): Thread delays the specified time run. Unit is seconds
import timeprint(‘sleep 3 seconds‘)time.sleep(3)print(‘醒了‘# 此句 隔3秒执行
5.time.asctime ([t]): A tuple or struct_time representing time is represented in this form: ' Sun Oct 1 12:04:38 2017 '. If there are no parameters, Time.localtime () will be passed in as a parameter.
>>>  a =  time.localtime () >>>  atime.struct_time (tm_year =  2018 , Tm_mon=  6 , Tm_mday =  30 , Tm_hour=  12 , Tm_min =  3 , Tm_sec=  39 , Tm_wday=  5 , Tm_yday=  181 , TM_ISDST =  0 ) >>>  time.asctime (a)  ' Sat June 12:03:39 2018 '   
>>> time.asctime()‘Sat Jun 30 12:03:08 2018‘
6.time.ctime ([secs]): Converts a timestamp (floating point number in seconds) to the form of Time.asctime (). If the parameter is not given or is none, the default Time.time () is the parameter. Its function is equivalent to Time.asctime (time.localtime (secs)).
>>>= time.time()>>> time.ctime(a)‘Sat Jun 30 12:44:07 2018‘>>> time.ctime()‘Sat Jun 30 12:44:18 2018‘
7. Time.strftime (format[, T]): Converts a tuple or struct_time that represents time, such as returned by Time.localtime () and Time.gmtime (), to a formatted time string. If T is not specified, the Time.localtime () is passed in.
>>>Time.strftime ('%y-%m-%d%h:%m:%s ')' 2018-06-30 13:05:07 '>>>A=Time.localtime ()>>>Atime.struct_time (tm_year=2018, Tm_mon=6, Tm_mday= -, Tm_hour= -, Tm_min=5, tm_sec= -, Tm_wday=5, Tm_yday=181, TM_ISDST=0)>>>Time.strftime ('%y-%m-%d%h:%m:%s 'A' 2018-06-30 13:05:14 '>>>Time.strftime ('%y-%m-%d%h:%m:%s%u ')' 2018-06-30 13:12:59 '

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
8. Strptime convert time in string form to Struct_time
>>> s = time.strftime(‘%Y-%m-%d %H:%M:%S‘)>>> s‘2018-06-30 13:17:20‘>>> time.strptime(s, ‘%Y-%m-%d %H:%M:%S‘)time.struct_time(tm_year=2018, tm_mon=6, tm_mday=30, tm_hour=13, tm_min=17, tm_sec=20, tm_wday=5, tm_yday=181, tm_isdst=-1)
9. Conversion between Time formats

Second, the DateTime module
    1. D=datetime.datetime.now () returns the current datetime date type
d.timestamp(),d.today(), d.year,d.timetuple()等方法可以调用

2.datetime.date.fromtimestamp (322222) Convert a timestamp to a datetime date type

3. Time arithmetic

>>>Datetime.datetime.now () Datetime.datetime (2018,6, -, -, $, Wu, the)>>>Datetime.datetime.now ()+Datetime.timedelta (1)#当前时间加一天Datetime.datetime (2018,7,1, -, -,5,187000)>>>Datetime.datetime.now () Datetime.datetime (2018,6, -, -, -, the,932000)>>>Datetime.datetime.now ()+Datetime.timedelta (Hours= 4)#当前时间加 4 hoursDatetime.datetime (2018,6, -, -, -, -,791000)

4. Time Replacement

>>>= datetime.datetime.now()>>> tdatetime.datetime(20186301405498000)>>>=1992)datetime.datetime(19926301405498000)

Python-time, Datetimme 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.