Python common time, datetime processing

Source: Internet
Author: User
Tags month name timedelta

There are three forms of time in Python:

float,

struct tuple (Time.struct_time or Datetime.datetime),

Str

Commonly used for:

float -to- struct tuple: time. localtime (float)

struct Time tuple --and 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-to-str: datetime. Strftime(format, datetime)

Str -- datetime: datetime.strptime(str, format)

datetime -to- struct time tuple: DateTime. Timetuple ()

Note:

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

DateTime is an int, omitting the millisecond part. DateTime tuple less than struct_time

1. Current time

>>> import time>>> time.time () 1450681042.751>>> time.localtime (Time.time ()) time.struct_ Time (tm_year=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 ())) ' 2015-12-21 15:01:28 ' >>> from datetime Import datetime>>> DateTime.Now () datetime.datetime (+, (+), (+), $, 279000) >>> Datetime.today () Datetime.datetime () (+, +, +,-A, 204000) >>> now = DateTime.Now () >>> Now.year, Now.month, Now.day, Now.hour, Now.minute, Now.second, now.microsecond>>> Now.isocalendar () # 2015 52nd Monday (Mon, 1) >>> Now.isoweekday () # Day of the week, 1:monday; and the Now.weekday () return value starts at 0 1

2. Date string--date

>>> s= ' 2015-12-21 15:01:28 ' >>> timetuple = Datetime.strptime (S, '%y-%m-%d%h:%m:%s ') Datetime.datetime, 1, (+) >>> 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-date formatting symbols in Python:  %y Two-digit year (00-99)  %y Four-digit year represents (000-9999)%m month (01-12)  the day of the Month  (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 locally Simplified month name  %b Local full month name  %c Local corresponding date representation and time represents  %j year of the Day (001-366)  %p local a.m. or p.m. equivalent of the  number of weeks in 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 indicates  %x local corresponding time represents  %Z the name of the current time zone  

3. Time stamp

>>> Time.mktime (Time.strptime (S, '%y-%m-%d%h:%m:%s ')) 1450681288.0>>> int (time.time ())
# Timestamp to time of 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 (, 6, Radix, 420000) DayOfweek = datetime.date Time.isoweekday () if DayOfweek = = 1: # Monday    last_time = Now + Datetime.timedelta (days=-3) Else:    Last_time = Now + Datetime.timedelta (Days=-1)


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

Python common time, datetime processing

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.