Python Date-time processing

Source: Internet
Author: User

Time Module
1 #-*-coding:utf-8-*-2 3 """4 #获取当前时间的时间戳 (per second)5 time.time ()6 7 #推迟指定秒数后再运行8 time.sleep (N)9 Ten #将时间元组转换为时间戳 One time.mktime (t) A  - #将时间戳转换为UTC + Time tuple for 0 time zones, default conversion time.time () - time.gmtime () the  - #将时间戳转换为当地时区的时间元组, default conversion time.time () - time.localtime () -  + #返回指定格式的时间格式 - time.strftime (format[, T]) +  A #将对应格式的时间字符串转换成时间元组 at time.strptime (string[, format]) - """ -  - Import Time -  - #time String --Timestamp inTime_tuple = Time.strptime ("2014-09-10 22:21:09","%y-%m-%d%h:%m:%s") - PrintTime.mktime (Time_tuple)#1410358869.0 to  + #time-stamped-time string -Time_tuple = Time.localtime (1410358869.0) the PrintTime.strftime ("%y-%m-%d%h:%m:%s", Time_tuple)#2014-09-10 22:21:09

DateTime module
1 #-*-coding:utf-8-*-2 3 """4 #时间对象5 time_obj = Datetime.time (hours, minutes, seconds)6 Time_obj.hour7 Time_obj.minute8 Time_obj.second9 Time_obj.microsecondTen  One #日期对象 A date_obj = datetime.date (year, month, day) - Date_obj.year - Date_obj.month the Date_obj.day -  - #将日期对象, time objects together - datetime.datetime.combine (date,time) +  - #当前日期的日期对象 + Datime.date.today () A  at  - #当前日期时间的日期时间对象 - Datetime.datime.now () - Datetime.datime.today () -  - #日期时间对象转换为时间元组 in date_time_obj = Datetime.dateime.now () - date_time_obj.timetuple () to  + #将时间戳转换为当地时区的日期时间对象 - Datetime.datetime.fromtimestamp (timestamp) the  * Date-time objects #将时间戳转换为UTC + 0 time zone $ Datetime.datetime.utcfromtimestamp (timestamp)Panax Notoginseng  - #日期时间运算 the Datetime.timedelta ([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, Weeks]] [ ]] +  A #当前日期时间减去8小时 the Datetime.datetime.now ()-Datetime.timedelta (hours=8) +  -  $ #返回指定格式的时间格式 $ datetime.datetime.strftime (format) -  - # #将对应格式的时间字符串转换为日期时间对象 the datetime.datetime.strptime (string, format) - Wuyi """ the  - Importdatetime Wu Import Time -  About #time String --Timestamp $Date_time_obj = Datetime.datetime.strptime ("2014-09-10 22:21:09","%y-%m-%d%h:%m:%s") - PrintTime.mktime (Date_time_obj.timetuple ())#1410358869.0 -  - #time-stamped-time string ADate_time_obj = Datetime.datetime.fromtimestamp (1410358869.0) + PrintDate_time_obj.strftime ("%y-%m-%d%h:%m:%s")#2014-09-10 22:21:09 the  - #a date time plus or minus one time $Date_time_obj2 = Date_time_obj-datetime.timedelta (days=8,hours=2) the  the PrintSTR (DATE_TIME_OBJ2)#2014-09-02 20:21:09 the PrintDate_time_obj2.strftime ("%y-%m-%d%h:%m:%s")#2014-09-02 20:21:09 the  - #compare two date and time in PrintDate_time_obj > Date_time_obj2#True the  the #Calculate two date time difference days, ignore time, compare dates only About Print(DATE_TIME_OBJ-DATE_TIME_OBJ2). Days#8

Python date-time processing

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.