Python common time conversion

import time

# Convert string to timestamp 
def str_to_timeStamp(str):
     ''' string format is: 2017-11-24 17:30:00 ''' 
    timeStruct = time.strptime(str, " %Y-%m-% d %H:%M:%S " )
    timeStamp = int(time.mktime(timeStruct))
    return timeStamp

#Convert the current time to a string 
def now():
     #Get the current time timestamp 
    now = int(time.time()) #Convert
     to other date formats, such as: "%Y-%m-%d %H:% M:%S" 
    timeStruct = time.localtime(now)
    strTime = time.strftime("%Y-%m-%d %H:%M:%S", timeStruct)
    return strTime

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324733631&siteId=291194637