Use of the Python time module and the DateTime module

Source: Internet
Author: User
Tags locale month name timedelta

Tag: Time () clock text datetime off tuple res--element

Time Module

Time Module

Time Representation format:

1.格式化时间: 【 2018-01-01 01:00:00 】2.时间戳: 【 1518407077.940927 】 -> 秒数(从1970开始到现今)3.以元组方式表示,九个元素(格式化元组)  元素如下:    (1)  tm_year=2018,  # 年    (2)  tm_mon=2,      # 月    (3)  tm_mday=12,    # 日    (4)  tm_hour=11,    # 小时    (5)  tm_min=49,     # 分钟    (6)  tm_sec=13,     # 秒    (7)  tm_wday=0,     # 0(Mon)-6(Sun) 从0(周一)开始数 -> 6(周天)    (8)  tm_yday=43,    # 今年的第几天    (9)  tm_isdst=0     # 时区,如果是1,则表示夏令时
Use
  • timezoneReturn time standard actual vs. current difference (seconds)

    In [45]: time.timezoneOut[45]: -28800In [46]: time.timezone / 3600Out[46]: -8.0   # 中国时间与标准时间相差8小时
  • altzoneand timezone almost

  • daylightTo see if you're using daylight saving time, 0 means not using

    In [50]: time.daylightOut[50]: 0
  • time ()Returns the current timestamp

    In [37]: time.time()Out[37]: 1518407588.8324711
  • sleep ()Pause the program for a few seconds

    In [51]: time.sleep(3)In [52]:
  • gmtime ()Incoming timestamp, return formatted tuple "UTC time Zone"

    In [54]: time.gmtime(time.time())Out[54]: time.struct_time(tm_year=2018, tm_mon=2, tm_mday=12, tm_hour=4, tm_min=7, tm_sec=50, tm_wday=0, tm_yday=43, tm_isdst=0)
  • localtime ()Incoming timestamp, return formatted tuple (no parameter passed, default current time) "based on local area"

    In [55]: time.localtime()Out[55]: time.struct_time(tm_year=2018, tm_mon=2, tm_mday=12, tm_hour=12, tm_min=10, tm_sec=13, tm_wday=0, tm_yday=43, tm_isdst=0)
    In [56]: time.localtime(123123123)Out[56]: time.struct_time(tm_year=1973, tm_mon=11, tm_mday=26, tm_hour=8, tm_min=52, tm_sec=3, tm_wday=0, tm_yday=330, tm_isdst=0)
To remove the tuple data:
  In [57]: a = time.localtime()  In [58]: a.tm_year # 取出年份  Out[58]: 2018  In [59]: a.tm_mon  # 取出月份  Out[59]: 2  In [60]: a.tm_sec # 取出分钟  Out[60]: 54
  • mktime ()Convert from tuple to timestamp

    In [63]: time.mktime(time.localtime())Out[63]: 1518409129.0
  • strftime (format,tuple)--> string Custom Generate format time, string

    1. Format operation

      % Y Ye Ar with century as a decimal number.
      %m Month as a decimal number [01,12].
      %d day of the month as a decimal number [01,31].
      % H Hour (24-hour clock) as a decimal number [00,23].
      % M Minute as a decimal number [00,59].
      % S Second as a decimal number [00,61].
      %z time zone offset from UTC.
      % A Locale ' s abbreviated weekday name.
      % A Locale ' s full weekday name.
      %b Locale ' s abbreviated month name.
      % B Locale ' s full month name.
      %c Locale ' s appropriate date and time representation.
      %I Hour (12-hour clock) as a decimal number [01,12].
      %p Locale ' s equivalent of either AM or PM

      2. Use: in []: Time.strftime ('%y-%m-%d ', Time.localtime ()) out[67]: ' 2018-02-12 '  
  • ctime ()Incoming timestamp, return text-time

    In [69]: time.ctime()Out[69]: ‘Mon Feb 12 12:30:44 2018‘In [77]: time.ctime(123123123)Out[77]: ‘Mon Nov 26 08:52:03 1973‘
  • asctime ()Incoming formatted tuple, return text-time
    In [79]: time.asctime(time.localtime(123123))Out[79]: ‘Fri Jan  2 18:12:03 1970‘
    In [80]: time.asctime()Out[80]: ‘Mon Feb 12 12:34:23 2018‘

DateTime module

Datetie Module
    • datetime.now ()Return Current time

      In [156]: print(datetime.datetime.now())In [156]: 2018-02-12 12:39:43.309016
Time arithmetic
In [174]: datetime.datetime.now() + datetime.timedelta(1)Out[174]: datetime.datetime(2018, 2, 13, 12, 43, 12, 301435)
In [175]: datetime.datetime.now() - datetime.timedelta(1)Out[175]: datetime.datetime(2018, 2, 11, 12, 43, 28, 154795)
In [181]: datetime.datetime.now() + datetime.timedelta(hours=1)Out[181]: datetime.datetime(2018, 2, 12, 13, 44, 54, 816476)
In [182]: datetime.datetime.now() + datetime.timedelta(hours=1)Out[182]: datetime.datetime(2018, 2, 12, 13, 44, 56, 429732)

Use of the Python time module and the DateTime module

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.