SoFunction
Updated on 2024-10-29

Based on python time processing method (detailed)

While working with data and performing machine learning, I encountered a large number of time series that needed to be processed. For example: the conversion of str and time for database reads, and the calculation of the difference of time. To summarize the time processing aspects of python.

I. Conversion of strings and time series

(): string=>time series

(): time series = > string

import time
start = "2017-01-01"
end = "2017-8-12"

startTime = (start,"%Y-%m-%d")
endTime = (end,"%Y-%m-%d") #The second parameter format specifies the format

print(startTime);print(endTime)

_start = ('%Y%m%d-%H:%M:%S',startTime)
_end = ('%Y%m%d-%H:%M:%S',endTime) # Parameters are in the opposite position
print(_start)
print(_end)

II. Time Stamping

(t): convert t to a timestamp

(s): convert timestamp to time

startStamp = (startTime) #time => timestamp
endStamp = (endTime)
print(startStamp)
print(endStamp)

_startTime = (startStamp) # Time stamp => time
print(_startTime)

III. Date operations

Note: the timestamps are in seconds from 1970, so our calculations can be based on the number of seconds

For example, here I am calculating the number of days between startTime and endTime:

print (int((endStamp-startStamp)/(24*60*60)))

IV. Format summary

%a Abbreviation for English week
%A English Weeks of Complete
%b Abbreviations for months in English
%B English month's complete
%c Display local date and time
%d dates,get1-31
%H hourly, 0-23
%I hourly, 0-12 
%m moon, 01 -12
%M minutes,1-59
%j Number of days on the day in the middle of the year
%w Show what day of the week it is
%W Weeks
%x 当天dates
%X Local time of day
%y particular year 00-99section of a room or lateral space between two pairs of pillars
%Y particular year的完整拼写

The above article based on python time processing methods (detailed) is all that I have shared with you, I hope to be able to give you a reference, and I hope you will support me more.