Python Date and Time Processing, python Date Processing

Source: Internet
Author: User
Tags timedelta

Python Date and Time Processing, python Date Processing

# Python time operations generally use the time and datetime modules

For the time module, there are three time representation modes.
1. timestamp: time. time ()
2. String: time. strftime ('% Y % m % D ')
3. struct_time format: time. localtime ()

As follows:

1 # time Operation 2 >>> import time 3 >>> time. time () 4 1450336566.81052 5 >>> time. localtime () 6 time. struct_time (tm_year = 2015, tm_mon = 12, tm_mday = 17, tm_hour = 15, tm_min = 16, tm_sec = 14, tm_wday = 3, tm_yday = 351, tm_isdst = 0) 7 >>> time. strftime ('% Y % m % d % H % M % s') 8' 20151217 151632 '9 >>> time. strptime ('2017 20151212 ',' % Y % m % d % H % M % s') 10 time. struct_time (tm_year = 2015, tm_mon = 12, tm_mday = 12, tm_hour = 12, tm_min = 12, tm_sec = 12, tm_wday = 5, tm_yday = 346, tm_isdst =-1) 11 >>> time. mktime (time. localtime () 12 1450336685.013 >>> 14 >>> yesterday = time. strftime ('% Y-% m-% d 00:00:00', time. localtime (time. time ()-3600*24) 15 >>> print yesterday16 >>>> tomorrow = time. strftime ('% Y-% m-% d 00:00:00', time. localtime (time. time () + 3600*24) 18 >>> print tomorrow19 00:00:00



Datetime is useful for time calculation.
There are several useful methods in the datetime module: datetime, date, time, timedelta
Syntax: datetime (year, month, day [, hour [, minute [, second [, microsecond [, tzinfo])
Date (year, month, day) --> date object
Time ([hour [, minute [, second [, microsecond [, tzinfo]) --> a time object
Timedelta (days = 1, hours = 2, minutes = 3, seconds = 4)
Calendar. monthrange (year, month): determines the month composed of year and month. The first day of the month is the day of the week and the total number of days of the month.

 

1 ## obtain date list 2 from datetime import datetime, date, timedelta 3 def get_range_time (begin, end, step): 4 while begin <end: 5 yield begin 6 begin = begin + step 7 8 for I in get_range_time (datetime (, 2), datetime (, 2), timedelta (days = 1 )): 9 print i10 11 from datetime import datetime, date, timedelta12 import calendar13 14 # obtain the dynamic month (the calendar month requires day = 1), as shown in the following 15 def get_month_range (startdate = None ): 16 if startdate is None: 17 startdate = date. today (). replace (day = 1) 18_, days_in_month = calendar. monthrange (startdate. year, startdate. month) 19 enddate = startdate + timedelta (days = days_in_month) 20 return startdate, enddate21 22 begin, end = get_month_range () 23 add_step = timedelta (days = 1) 24 while begin <end: 25 print begin26 begin = begin + add_step

 

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.