1. 程式人生 > >python CST中國標準時間格式轉換

python CST中國標準時間格式轉換

def trans_format(time_string, from_format, to_format='%Y.%m.%d %H:%M:%S'):
    """
    @note 時間格式轉化
    :param time_string:
    :param from_format:
    :param to_format:
    :return:
    """
    time_struct = time.strptime(time_string,from_format)
    times = time.strftime(to_format, time_struct)
    
return times
if __name__ == "__main__":
    cst_time="Tue Nov 06 00:00:00 CST 2018"
    format_time=trans_format(cst_time,'%a %b %d %H:%M:%S CST %Y', '%Y-%m-%d %H:%M:%S')
    print format_time

輸出結果:

2018-11-06 00:00:00