Python commonly used time, datetime processing, pythondatetime

Source: Internet
Author: User
Tags month name timedelta

Python commonly used time, datetime processing, pythondatetime

In python, time has three formats:

Float,

Struct tuple(Time. struct_time or datetime. datetime ),

Str

 

Common:

Float-->Struct tuple: Time.Localtime(Float)

Struct time tuple-->Str: Time.Strftime(Format,Struct time tuple)

Str-->Struct time tuple: Time.Strptime(Str, format)

Struct time tuple-->Float: Time.Mktime(Struct time tuple)

Struct time tuple-->Datetime:Datetime (* time_tuple [0: 6])

 

Float-->Datetime: Datetime. datetime.Fromtimestamp(Float)

Datetime --> str: Datetime. Strftime(Format,Datetime)

Str-->Datetime:Datetime. strptime(Str, format)

Datetime-->Struct time tuple: Datetime.Timetuple()

 

Note:

Time is based on float, the decimal point is millisecond, And the integer part is second. (Java is millisecond, so, python_time * 1000 = Java_time)

Datetime is int, Which is omitted in milliseconds. Datetime tuple is less than struct_time

 

1. Current Time

>>> Import time >>> time. time () 1450681042.751 >>> time. localtime (time. time () time. struct_time (maid = 2015, tm_mon = 12, tm_mday = 21, tm_hour = 15, tm_min = 0, tm_sec = 2, tm_wday = 0, tm_yday = 355, tm_isdst = 0) >>> time. strftime ('% Y-% m-% d % H: % M: % s', time. localtime (time. time () '2017-12-21 15:01:28 '>>> from datetime import datetime >>> datetime. now () datetime. datetime (2015, 12, 21, 14, 58, 38,279 000)> datetime. today () datetime. datetime (2015, 12, 21, 14, 59, 20,204 000) >>> now = datetime. now () >>> now. year, now. month, now. day, now. hour, now. minute, now. second, now. microsecond >>> now. isocalendar () # Monday, week 1, 52nd (2015, 52, 1) >>> now. isoweekday () # day of the week, 1: Monday; and now. the returned value of weekday () starts from 0 and 1.

 

2. Date string --> date

>>> s='2015-12-21 15:01:28'>>> timeTuple = datetime.strptime(s, '%Y-%m-%d %H:%M:%S')datetime.datetime(2015, 12, 21, 15, 1, 28)>>> datetime.datetime.strftime('%Y/%m/%d %H:%M:%S', timeTuple)

>>> s='2015-12-21 15:01:28'>>> timeTuple = datetime.strptime(s, '%Y-%m-%d %H:%M:%S')time.struct_time(tm_year=2015, tm_mon=12, tm_mday=21, tm_hour=15, tm_min=1, tm_sec=28, tm_wday=0, tm_yday=355, tm_isdst=-1)>>> time.strftime('%Y/%m/%d %H:%M:%S',timeTuple)
Time and date formatting symbol in python: % y two-digit year representation (00-99) % Y four-digit year representation (000-9999) % m month (01-12) one day (0-31) % H 24-hour (0-23) % I 12-hour (01-12) in % d month) % M minutes (00 = 59) % S seconds (00-59) % a local simplified week name % A local full week name % B local simplified month name % B local full month name % c local corresponding date representation and Time Representation % j Years one day (001-366) % p local. m. or P. m. % U the number of weeks in a year (00-53) Sunday is the beginning of the week % w Week (0-6 ), sunday is the beginning of a week % W the number of weeks in a year (00-53) monday is the start of the week % x local corresponding date represents % X local corresponding time represents % Z Current Time Zone name % itself

 

3. Timestamp

>>> time.mktime(time.strptime(s,'%Y-%m-%d %H:%M:%S'))1450681288.0>>> int(time.time())
# timestamp to time tuple in UTCtimestamp = 1226527167.595983time_tuple = time.gmtime(timestamp)print repr(time_tuple)
# timestamp to time tuple in local timetimestamp = 1226527167.595983time_tuple = time.localtime(timestamp)print repr(time_tuple)

 

2. Date addition and subtraction

import datetimenow = datetime.datetime.now() # datetime.datetime(2015, 12, 16, 15, 6, 37, 420000)dayOfweek = datetime.datetime.isoweekday()if dayOfweek == 1: # Monday    last_time = now + datetime.timedelta(days=-3)else:    last_time = now + datetime.timedelta(days=-1)


Turn: http://www.cnblogs.com/snow-backup/p/5063665.html

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.