Python3 time module and datetime module, python3datetime

Source: Internet
Author: User
Tags month name timedelta

Python3 time module and datetime module, python3datetime

Time Module

In Python, these methods are usually used to represent the time: 1) timestamp 2) formatted time string 3) there are nine elements in the tuples (struct_time. Since the time module of Python mainly calls the C library, different platforms may be different.

UTC (Coordinated Universal Time) is the Greenwich mean Time, which is the world standard Time. UTC + 8 for Beijing, China.
DST (Daylight Saving Time) is the Daylight Saving Time.
Timestamp: Generally, the timestamp refers to the second of GMT, January 1, January 01, 1970 (, January 1, January 01, 1970, Beijing time) the total number of seconds from now on. We run "type (time. time ()" and return the float type. Functions that return timestamp mainly include time () and clock.
_Time: there are nine elements in struct_time. The main functions that return struct_time include gmtime (), localtime (), and strptime ().

The following lists the elements in this method:

 

1. returns the current local time in the form of tuples >>> time. localtime () time. struct_time (tm_year = 2017, tm_mon = 5, tm_mday = 8, tm_hour = 16, tm_min = 13, tm_sec = 34, tm_wday = 0, tm_yday = 128, tm_isdst = 0) 2. returns Greenwich Mean time in the form of tuples> time. gmtime () time. struct_time (tm_year = 2017, tm_mon = 5, tm_mday = 8, tm_hour = 8, tm_min = 13, tm_sec = 38, tm_wday = 0, tm_yday = 128, tm_isdst = 0) 3. convert the time of the tuples to the timestamp >>> x = time. localtime () >>> time. mktime (x) 1494232890. 04. convert the time of the tuples to the time of the string format >>> x = time. localtime () >>> time. strftime ('% Y-% m-% d % H: % M: % s', x) '2017-05-08 16:57:38' 5. convert string format time to tuples format time> time. strptime ('2017-05-08 17:03:12 ',' % Y-% m-% d % H: % M: % s') time. struct_time (tm_year = 2017, tm_mon = 5, tm_mday = 8, tm_hour = 17, tm_min = 3, tm_sec = 12, tm_wday = 0, tm_yday = 128, tm_isdst =-1) 6. time in the format of tuples is converted to the time in the string format >>> time. asctime () 'tue May 9 15:23:21 2017 '>>> x = time. localtime () >>> time. asctime (x) 'tue May 9 15:23:39 2017 '7. time stamp converted to string format time >>> time. ctime () 'tue May 9 16:07:24 2017 '>>> time. ctime (987867475) 'sat Apr 21 23:37:55 123'

 

 

1. For the format, refer to 2% a local (locale) simplified week name 3% A local full week name 4% B local simplified month name 5% B local full month name 6% c Local corresponding date and time represents 7% d the day of the month (01- 31) hour (in 24-hour format, 00-23) in 8% H day 9% I hour (in 12-hour format, 01-12) 10% j the day of the year (001-366) 11% m month (01-12) 12% M minutes (00-59) 13% p local am or pm corresponding to 14% S seconds (01-61) 15% U the number of weeks in a year. (00-53 Sunday is the beginning of a week .) All days before the first Sunday are placed in week 0th. 16% w the day of a week (0-6, 0 is Sunday) 17% W and % U are basically the same, the difference is that % W starts from Monday as a week. 18% x local date 19% X local time 20% y remove century year (00-99) 21% Y complete year 22% Z Time Zone name (if not null characters) 23% %'

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Datetime Module

 

Import datetime1. return the current time >>> datetime. datetime. now () datetime. datetime (2017, 5, 9, 17, 7, 0, 514481) 2. time stamp to date >>> datetime. date. fromtimestamp (1178766678) datetime. date (2007, 5, 10) 3. current Time + 3 days> datetime. datetime. now () + datetime. timedelta (+ 3) datetime. datetime (2017, 5, 12, 17, 12, 42,124 379) 4. current Time-3 days> datetime. datetime. now () + datetime. timedelta (-3) datetime. datetime (2017, 5, 6, 17, 13, 18,474) 5. current Time + 3 hours> datetime. datetime. now () + datetime. timedelta (hours = 3) datetime. datetime (2017, 5, 9, 20, 13, 55,678 310) 6. current Time + 30 minutes> datetime. datetime. now () + datetime. timedelta (minutes = 30) datetime. datetime (2017, 5, 9, 17, 44, 40,392 370)

 

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.