SoFunction
Updated on 2024-10-28

Python Timestamp to String and String to Timestamp Conversions

Instance code:

if __name__ == '__main__':
    # Time stamp
    import time
    seconds = ()
 
    # Convert timestamps to strings
    print(("%Y-%m-%d %H:%M:%S", (seconds)))
 
    # String to timestamp conversion
    print((("2018-08-07", "%Y-%m-%d")))
    if len(df) > 0:
        # String to timestamp conversion
        x = list(df['Date'])
        x1 = [((c, "%Y-%m-%d")) for c in x]
        df['Date Time Stamp'] = x1
        df.sort_values(by='Date Time Stamp', inplace=True, ascending=True)
 

Comprehensive examples

This code converts a string to a time format, and a time format to a string.

pm_str = '6/18/2017 9:52:37 PM'
stTemp = (pm_str, "%m/%d/%Y %I:%M:%S %p")
print(stTemp)
str_time = ("%m-%d-%Y_%H%M%S", stTemp)
print(str_time)

The output of stTemp is

time.struct_time(tm_year=2017, tm_mon=6, tm_mday=18, tm_hour=21, tm_min=52, tm_sec=37, tm_wday=6, tm_yday=169, tm_isdst=-1)

The output of str_time is 06-18-2017_215237

summarize

to this article on the Python timestamp conversion to string and string conversion to timestamp article is introduced to this, more related Python timestamp conversion to string content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!