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