Realization Method of Conversion of Time Date and Time Stamp in python

  • 2021-07-10 20:04:48
  • OfStack

1. Introduction

When writing code, it often involves the conversion of time, date and timestamp.

2. Examples


#  Introducing module 
import time, datetime

2.1 Convert date of str type to timestamp


#  Time of character type 
tss1 = '2013-10-10 23:40:00'
#  Convert to time array 
timeArray = time.strptime(tss1, "%Y-%m-%d %H:%M:%S")
print timeArray   
# timeArray You can call tm_year Etc 
print timeArray.tm_year  # 2013
#  Convert to timestamp 
timeStamp = int(time.mktime(timeArray))
print timeStamp # 1381419600


#  The results are as follows 
time.struct_time(tm_year=2013, tm_mon=10, tm_mday=10, tm_hour=23, tm_min=40, tm_sec=0, tm_wday=3, tm_yday=283, tm_isdst=-1)
2013
1381419600

2.2 Change the display format of str type dates


tss2 = "2013-10-10 23:40:00"
#  Convert to Array 
timeArray = time.strptime(tss2, "%Y-%m-%d %H:%M:%S")
#  Convert to other display formats 
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print otherStyleTime # 2013/10/10 23:40:00

tss3 = "2013/10/10 23:40:00"
timeArray = time.strptime(tss3, "%Y/%m/%d %H:%M:%S")
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print otherStyleTime # 2013-10-10 23:40:00

2.3 The timestamp is converted to a date in the specified format


#  Use time
timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
print otherStyleTime  # 2013--10--10 23:40:00
#  Use datetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
print otherStyleTime  # 2013--10--10 15:40:00

2.4 Gets the current time and displays it in the specified format


# time Get the current timestamp 
now = int(time.time())   # 1533952277
timeArray = time.localtime(now)
print timeArray
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
print otherStyleTime  

#  The results are as follows 
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=11, tm_hour=9, tm_min=51, tm_sec=17, tm_wday=5, tm_yday=223, tm_isdst=0)
2018--08--11 09:51:17


# datetime Gets the current time, array format 
now = datetime.datetime.now()
print now
otherStyleTime = now.strftime("%Y--%m--%d %H:%M:%S")
print otherStyleTime 

#  The results are as follows: 
2018-08-11 09:51:17.362986
2018--08--11 09:51:17

The original string is matched in time format by datetime. datetime. strptime (date_string, format), and assigned to time_format, then time_format calls strftime (format) function to output its desired format

Time and date formatting symbols in python:

% y two-digit year representation (00-99)

% Y 4-digit year representation (0000-9999)

% m month (01-12)

1 day in% d month (0-31)

% H 24 Hours (0-23)

% I 12 Hours (01-12)

% M minutes (00-59)

% S seconds (00-59)

% a Local Simplified Week Name

% A local full week name

% b Local Simplified Month Name

% B local full month name

% c local corresponding date and time representations

1 day in% j year (001-366)

Equivalent for% p local A. M. or P. M

% U the number of weeks in a year (00-53) Sunday is the beginning of the week

% w week (0-6), Sunday is the beginning of the week

% W Number of weeks in a year (00-53) Week 1 is the beginning of the week

% x local corresponding date representation

% X local corresponding time representation

% Z the name of the current time zone

%%% number itself


Related articles: