Python uses timestamps

Source: Internet
Author: User
Tags string format timedelta

1. Convert the time of a string to a timestamp method: a = "2013-10-10 23:40:00" to convert it to a time array importtime Timearray = Time.strptim E (A, "%y-%m-%d%h:%m:%s") converted to timestamp: timeStamp = Int (time.mktime (timearray)) TimeStamp = = 13814196002. String format changes such as a = "2013-10-10 23:40:00", want to change to a = "2013/10/10 23:40:00" method: first converted to an array of time and then converted to another format Timearray = Time.strptime (A, "%y-%m-%d% h:%m:%s ") Otherstyletime = Time.strftime ("%y/%m/%d%h:%m:%s ", Timearray) 3. Timestamp is converted to the specified format date: Method One: Use the localtime () to Change to a time array, and then format the required format, such as TimeStamp = 1381419600 Timearray = time.localtime (timeStamp) otherstyletime = Ti        Me.strftime ("%y-%m-%d%h:%m:%s", timearray) Otherstyletime = = "2013-10-10 23:40:00" method two: Importdatetime TimeStamp = 1381419600 Datearray = Datetime.datetime.utcfromtimestamp (timeStamp) otherstyletime = Dat Earray.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: Impo Rttime get the current time time stamp       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 (timesta        MP) Otherstyletime = Time.strftime ("%y-%m-%d%h:%m:%s", Timearray) method two: Importdatetime get 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: Importtime importdatetime first gets the date of the time array format Threedayago = (datetime.datetime.no        W ()-Datetime.timedelta (days = 3)) converted to timestamp: timeStamp = Int (Time.mktime (Threedayago.timetuple ())) Convert to a different string format: Otherstyletime = Threedayago.strftime ("%y-%m-%d%h:%m:%s") Note: Timedelta () The number of references is: Days,hours,seconds , microseconds 6. For a given timestamp, calculate the time of the day before time: TimeStamp = 1381419600 First converted to datetime importdatetime importtime Datearray = Datetime.datetime.utcfromtimestamp (timeStamp) Threedayago = Datearray-datetime.timedelta (days = 3) 5, can be converted to other Format of the Italian




Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Python uses timestamps

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.