Python time, date, time stamp conversion between

Source: Internet
Author: User
Tags string format timedelta

#1. Converts the time of a string to a timestamp method: a = "2013-10-10 23:40:00" #将其转换为时间数组import Timetimearray = Time.strptime (A, "%y-%m-%d%h:%m:%s") # Convert to timestamp: timeStamp = Int (time.mktime (timearray)) TimeStamp = = 1381419600# string format changed as a = "2013-10-10 23:40:00", want to change to a = "2013/10 /10 23:40:00 "# Method: First convert to an array of time and then convert to another format Timearray = Time.strptime (A,"%y-%m-%d%h:%m:%s ") Otherstyletime = Time.strftime ("% y/%m/%d%h:%m:%s ", Timearray) # 3. The timestamp is converted to the specified format date: # method One: Use the 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: Import Datetimetimestamp = 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: Import time# get current time timestamp now = Int (Time.time ()) #这是时间戳转换为其 His 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: Import datetime# get current time now = Datetime.datetime.now () #这是时间数组格式转换为指定的格式: Otherstyletime = Now.strftime ( "%y-%m-%d%h:%m:%s") # 5. Get three days ago # method: Import Timeimport datetime# first gets the time array format Date Threedayago = (Datetime.datetime.now ()-Datetime.timedelta (days= 3) # Convert to timestamp: timeStamp = Int (Time.mktime (Threedayago.timetuple ())) # converted to a different string format: Otherstyletime = Threedayago.strftime ( "%y-%m-%d%h:%m:%s") # Note: The parameters of Timedelta () are: days, hours, seconds, microseconds# 6. Given a timestamp, calculate the time before the time: TimeStamp = 1381419600 # First converted to datetimeimport datetimeimport Timedatearray = Datetime.datetime.utcfromtimestamp (timeStamp) Threedayago = Datearray-datetime.timedelta (days=3)

  

Python time, date, time stamp conversion between

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.