1. Introduction
When writing code, it often involves converting times, dates, and timestamps to each other.
2. Examples
# Introducing Modules import time, datetime
2.1 Converting dates of type str to timestamps
# Character type time tss1 = '2013-10-10 23:40:00' # Converted to a time array timeArray = (tss1, "%Y-%m-%d %H:%M:%S") print timeArray # timeArray can call tm_year etc. print timeArray.tm_year # 2013 # Converted to timestamp timeStamp = int((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 Changing the display format of dates of type str
tss2 = "2013-10-10 23:40:00" # Convert to an array timeArray = (tss2, "%Y-%m-%d %H:%M:%S") # Convert to other display formats otherStyleTime = ("%Y/%m/%d %H:%M:%S", timeArray) print otherStyleTime # 2013/10/10 23:40:00 tss3 = "2013/10/10 23:40:00" timeArray = (tss3, "%Y/%m/%d %H:%M:%S") otherStyleTime = ("%Y-%m-%d %H:%M:%S", timeArray) print otherStyleTime # 2013-10-10 23:40:00
2.3 Converting timestamps to dates in a specified format
# Use time timeStamp = 1381419600 timeArray = (timeStamp) otherStyleTime = ("%Y--%m--%d %H:%M:%S", timeArray) print otherStyleTime # 2013--10--10 23:40:00 # Use datetime timeStamp = 1381419600 dateArray = (timeStamp) otherStyleTime = ("%Y--%m--%d %H:%M:%S") print otherStyleTime # 2013--10--10 15:40:00
2.4 Getting the current time and displaying it in a specified format
# timeGet the current timestamp now = int(()) # 1533952277 timeArray = (now) print timeArray otherStyleTime = ("%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 # datetimeGet the current time, in array format now = () print now otherStyleTime = ("%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
Through the (date_string, format) will be the original string for the time format matching, and assigned to the time_format, then time_format call strftime (format) function, the output of their own desired format
Time-date formatting notation in python:
%y Two-digit year indication (00-99)
%Y Four-digit year indication (0000-9999)
%m Month (01-12)
%d Day of the month (0-31)
%H 24-hour clock hours (0-23)
%I 12-hour hours (01-12)
%M minutes (00-59)
%S seconds (00-59)
%a Local simplified weekly name
%A Local full week name
%b Local simplified name of the month
%B Local full month name
%c Local corresponding date representation and time representation
%j Day of the year (001-366)
%p Local. or . Equivalents of
%U Number of weeks in a year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday is the start of the week
%W Number of weeks in a year (00-53) Monday is the start of the week
%x Local representation of the corresponding date
%X Local corresponding time representation
%Z Name of the current time zone
%% % sign itself
This is the whole content of this article, I hope it will help you to learn more.