字符串转成时间戳

强烈推荐:

1、日期字串转为时间戳int:https://www.cnblogs.com/andy9468/p/12627534.html

2、时间戳转为日期字串:https://www.cnblogs.com/andy9468/p/12627597.html

3、比较2个时刻日期字串的时间差:https://www.cnblogs.com/andy9468/p/12627663.html

4、比较2个时刻日期字串的时间差:距离现在的时间距离(不同时间格式)https://www.cnblogs.com/andy9468/p/12627927.html

5、python获取一年所有的日期: https://www.cnblogs.com/andy9468/p/10710142.html

=====================================================================

(一)通用:time包

1、日期字符串 => 时间戳int

import time

# 将时间字符串转为时间戳int
dt = "2016-05-05 20:28:54"
# 转换成时间数组
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
# 转换成时间戳
timestamp = time.mktime(timeArray)

print(timeArray)
print("时间戳-timestamp: %s" % timestamp)

  

封装好的函数1:日期 转为 时间戳。单位s,秒。

def datestr2timeint(date_str='2016-05-05 20:28:54', format='%Y-%m-%d %H:%M:%S'):
    '''
    日期字符串 转为 时间戳。精确到s,单位秒。
    输入举例说明:
    ('2016-05-05 20:28:54')
    ('2016-05-05 20:28:54','%Y-%m-%d %H:%M:%S')
    ('20160505 20:28:54','%Y%m%d %H:%M:%S')
    ('20160505 20_28_54','%Y%m%d %H_%M_%S')
    ('20160505','%Y%m%d')
    :param date_str:日期字符串
    :param format:输入日期字串的日期格式、样式
    :return:转换为int的时间戳
    '''
    # 将时间字符串转为时间戳int
    dt = date_str
    # 转换成时间数组
    timeArray = time.strptime(dt, format)
    # 转换成时间戳
    timestamp = int(time.mktime(timeArray))

    return timestamp

  

2、时间戳 => 日期字符串

(1)精确到秒s

import time

# 将时间戳int转为时间字符串
timestamp = 1462451334
# 转换成localtime
time_local = time.localtime(timestamp)
# 转换成新的时间格式(2016-05-05 20:28:54)
dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
print(dt)
print("日期字符-dt: %s" % dt)

 封装好的函数2.1:时间戳 转为 日期字符串。单位s,秒。

def timestamp2date(timestamp=1565673941, format="%Y-%m-%d %H:%M:%S"):
    '''
    时间戳转为日期字串,单位s,秒
    :param timestamp:时间戳
    :return:日期字串
    输出举例说明:
    (1565673941, "%Y-%m-%d %H:%M:%S")  输出  2019-08-13 13:25:41
    (1565673941, "%Y-%m-%d")  输出  2019-08-13
    (1565673941, "%Y%m%d")  输出  20190813
    '''
    local_time = time.localtime(timestamp)
    data_head = time.strftime(format, local_time)

    return data_head

  

(2)精确到毫秒ms 

import time

ct = time.time()
# ct = 1536994867.9991837
print(ct)
print(int(ct))
local_time = time.localtime(ct)
data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
data_secs = (ct - int(ct)) * 1000
dt_ms = "%s.%03d" % (data_head, data_secs)
print(dt_ms)

  封装好的函数2.2:时间戳 转为 日期字串。单位ms,毫秒。

def timestamp2datems(timestamp):
    '''
    时间戳转为日期字串,精确到ms。单位s
    :param timestamp:时间戳
    :return:日期字串
    '''
    local_time = time.localtime(timestamp)
    data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
    data_secs = (timestamp - int(timestamp)) * 1000
    dt_ms = "%s.%03d" % (data_head, data_secs)
    # print(dt_ms)
    return dt_ms

  

3、获取当前时间的时间戳

time.time()

  

 (二)其他:datetime包

1. 日期输出格式化 datetime => string

import datetime
now = datetime.datetime.now()
now.strftime('%Y-%m-%d %H:%M:%S')

strftime是datetime类的实例方法(即为实例方法)。

2. 日期输出格式化 string => datetime

import datetime
t_str = '2015-04-07 19:11:21'
d = datetime.datetime.strptime(t_str, '%Y-%m-%d %H:%M:%S')

 strptime是datetime类的静态方法。

3.任意格式时间字符串转成时间戳

import datetime
GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
TIME = 'Thu, 19 Feb 2009 16:00:07 GMT'
timestamp = datetime.datetime.strptime(TIME, GMT_FORMAT)
print(timestamp)

  

(三)日期字串转换时间戳,日期字串秒数差值

import time


def datestr2timeint(date_str='2016-05-05 20:28:54', format='%Y-%m-%d %H:%M:%S'):
    '''
    日期字符串 转为 时间戳。精确到s,单位秒。
    输入举例说明:
    ('2016-05-05 20:28:54')
    ('2016-05-05 20:28:54','%Y-%m-%d %H:%M:%S')
    ('20160505 20:28:54','%Y%m%d %H:%M:%S')
    ('20160505 20_28_54','%Y%m%d %H_%M_%S')
    ('20160505','%Y%m%d')
    :param date_str:日期字符串
    :param format:输入日期字串的日期格式、样式
    :return:转换为int的时间戳
    '''
    # 将时间字符串转为时间戳int
    dt = date_str
    # 转换成时间数组
    timeArray = time.strptime(dt, format)
    # 转换成时间戳
    timestamp = int(time.mktime(timeArray))

    return timestamp


def time_str_minus_seconds(old_date, now_date):
    '''
    :param old_date: 过去日期字串
    :param now_date: 现在日期字串
    :return: 时间差值,秒
    '''
    stamp_ago = datestr2timeint(old_date)
    stamp_now = datestr2timeint(now_date)
    minus_seconds = stamp_now - stamp_ago
    # 返回差的秒数
    return minus_seconds


if __name__ == '__main__':
    time_str1 = "2019-08-12 13:25:41"
    time_str2 = "2019-08-13 13:25:41"
    print(datestr2timeint(time_str1))
    print(datestr2timeint(time_str2))
    print(time_str_minus_seconds(time_str1, time_str2))

  

原文地址:https://www.cnblogs.com/andy9468/p/7992261.html