Python timestamp usage

Source: Internet
Author: User
Tags timedelta

1. convert string time to timestamp: a = "23:40:00" to the time array importtime timeArray = time. strptime (a, "% Y-% m-% d % H: % M: % S") is converted to the timeStamp: timeStamp = int (time. mktime (timeArray) timeStamp = 13814196002. to change the string format to a = "23:40:00", convert it to a = "23:40:00". Method: convert it to a time array, and then convert it to another format: timeArray = time. strptime (a, "% Y-% m-% d % H: % M: % S") otherStyleTime = time. strftime ("% Y/% m/% d % H: % M: % S", timeArray) 3. time stamp conversion to the specified format Date: Method 1: Use localtime () to convert to the time array, and then format it into the required format, such as timeStamp = 1381419600 timeArray = time. localtime (timeStamp) otherStyleTime = time. strftime ("% Y-% m-% d % H: % M: % S", timeArray) otherStyletime = "2013-10-10 23:40:00" Method 2: importdatetime timeStamp = 1381419600 dateArray = datetime. datetime. utcfromtimestamp (timeStamp) otherStyleTime = dateArray. strftime ("% Y-% m-% d % H: % M: % S") otherStyletime = "2013-10-10 23:40:00" 4. obtain the current time and convert it to the specified date format Method 1: importtime get 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 2: importdatetime get the 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. obtain the time method three days ago: importtime importdatetime first obtains the date threeDayAgo = (datetime. datetime. now ()-datetime. timedelta (days = 3) to timeStamp: timeStamp = int (time. mktime (threeDayAgo. timetuple () to other string formats: otherStyleTime = threeDayAgo. strftime ("% Y-% m-% d % H: % M: % S") Note: timedelta () parameters include: days, hours, seconds, microseconds 6. given a timeStamp, calculate the time a few days ago: timeStamp = 1381419600 first converted to datetime importdatetime importtime dateArray = datetime. datetime. utcfromtimestamp (timeStamp) threeDayAgo = dateArray-datetime. timedelta (days = 3) refer to 5, which can be converted to any other format




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.