Conversion of time object generation and time format in Python

Source: Internet
Author: User
Tags local time timedelta

1. Convert the time of a string to a timestamp    method        :"2013-10-10 23:40:00" converts         it to a time array         import Time "%y-%m-%d%h:%m:%s") converted to timestamp            :    = int (time.mktime (timearray))    = = 1381419600
2. String format changes such as a="2013-10-10 23:40:00", want to change to a ="2013/10/10 23:40:00"method: Convert to a time array first and then convert to a different format Timearray= Time.strptime (A,"%y-%m-%d%h:%m:%s") Otherstyletime= Time.strftime ("%y/%m/%d%h:%m:%s", Timearray)3The time stamp is converted to the specified format date: Method One: Use LocalTime () to convert to a time array, and then format it to the desired format, such as TimeStamp= 1381419600Timearray=time.localtime (timeStamp) otherstyletime= Time.strftime ("%y-%m-%d%h:%m:%s", Timearray) otherstyletime=="2013-10-10 23:40:00"Method Two:Importdatetime timeStamp= 1381419600Datearray=Datetime.datetime.utcfromtimestamp (timeStamp) otherstyletime= Datearray.strftime ("%y-%m-%d%h:%m:%s") Otherstyletime=="2013-10-10 23:40:00"4. Gets the current time and converts to the specified date format method one:Importtime Gets the current time timestamp now= Int (Time.time ())This is the time stamp converted to another date format, such as:"%y-%m-%d%h:%m:%s"Timearray=time.localtime (timeStamp) otherstyletime= Time.strftime ("%y-%m-%d%h:%m:%s", Timearray) method two:Importdatetime gets current time now= Datetime.datetime.now ()This is the time array format converted to the specified format: Otherstyletime= Now.strftime ("%y-%m-%d%h:%m:%s") 5. Get three days before the time method:Import TimeImportdatetime First gets the date of the time array format Threedayago= (Datetime.datetime.now ()-Datetime.timedelta (days = 3) ) Convert to timestamp: TimeStamp=Int (Time.mktime (Threedayago.timetuple ())) is converted to another string format: Otherstyletime= Threedayago.strftime ("%y-%m-%d%h:%m:%s"Note: The parameters of Timedelta () are: Days,hours,seconds,microseconds6. Given a timestamp, calculate the time of the day before time: TimeStamp= 1381419600Convert to datetime firstImportdatetimeImportTime Datearray=Datetime.datetime.utcfromtimestamp (timeStamp) Threedayago= Datearray-datetime.timedelta (days = 3) Reference 5, can be converted to any other format.

%a Shorthand for English Week Mon
%A Complete writing of English week Monday
%b Abbreviations for English months June
%B Complete writing of the English month June
%c Displays the local date and time 06/30/14 01:03:17
%I Number of hours, value between 01~12 01
%j Displays the number of days from the first day of the year to the day 181
%w (week) Show today is the day of the week, 0 means Sunday 1
%W Show the week of the year, Monday as the first day of the week ==> 26*7 = 182 days
%x Local date of day 06/30/14
%x local time of day 01:09:54
%y Year, value between 00~99 14
%Y Complete spelling of the year 2014
%m (month) month, the value is between 01~12 06
%d (day) Number of dates, value between 1~31 30
%H (Hour) Number of hours, value between 00~23 01
%M (Minute) Number of minutes, value between 01~59 04
%s Seconds 58

Conversion of time object generation and time format in Python

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.