#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