Python Basic Learning Time and DateTime module

Source: Internet
Author: User

Time Module Timing and datetime ()
Time is represented in three formats 1. Timestamp 2. Time tuple 3. Formatted string (2017-11-1)
Timestamp: Typically, a timestamp represents an offset that is calculated in seconds from 00:00:00 Greenwich on January 1, 1970
Time zone: World Standard Time UTC (Greenwich Astronomical Time)
China time for Utc+8 says East eight district
DST: Daylight Saving Time


Import time

Time.time () #获取当前的时间戳
Time.timezone () #查看当前的本地时间和世界标准时间的差值 (seconds)
Time.daylight () #查看当地时间是否启动了夏令时
Time.sleep () #休息 (seconds)


Conversion between timestamps and time tuples

Time.gmtime () #获取一个当前UTC时间元组, parameter is timestamp, the current timestamp is used by default when no parameter
Time.local () #获取一个当前的本地时间元祖, the parameter is timestamp. Use the current timestamp by default when no parameters are available
Time.mktime () #将一个时间元组转换为时间戳

Conversion between a time tuple and a formatted string
Time.strftime (format,tuple) #将时间元祖转换为格式化字符串
Format%y%m Month%d day%h (24 o'clock)%m minutes%s seconds
For example: Time.strftime ("%y-%m-%d%h:%m:%s", tuple) in the format: 2017-11-1 12:48:20
Time.strptime (string, format) #将格式化字符转换为时间元组
Time.asctime () #将一个时间元祖转换为: Sun Nov 5 12:53:03 2017 formatted string
Time.ctime () #将一个时间戳转换为: Sun Nov 5 12:53:03 2017 formatted string


Import datetime

Datetime.datetime.now () #获取当前的时间, format: 2017-11-05 12:58:08.998633
Datetime.date.fromtimestamp #将时间戳转换为格式: 2017-11-05

Time Plus Subtraction
Datetime.datetime.now () + Datetime.timedelta (3) #将当前日期 + 3 days (negative for 3 days)
Datetime.datetime.now () + Datetime.timedelta (minutes=30) #将当前时间 + 30 mins
Datetime.datetime.now () + Datetime.timedelta (hours=3) #将当前时间 + 3 hours

Modify the current time
C_time = Datetime.datetime.now ()
C_time.replace (minute=30,hours=2)

Python Basic Learning Time and DateTime 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.