Python获取昨天、今天、明天开始、结束时间戳

Python获取昨天、今天、明天开始、结束时间戳

# coding=utf-8
import time
import datetime

# 今天日期
today = datetime.date.today()

# 昨天时间
yesterday = today - datetime.timedelta(days=1)

# 明天时间
tomorrow = today + datetime.timedelta(days=1)

acquire = today + datetime.timedelta(days=2)

# 昨天开始时间戳
yesterday_start_time = int(time.mktime(time.strptime(str(yesterday), '%Y-%m-%d')))

# 昨天结束时间戳
yesterday_end_time = int(time.mktime(time.strptime(str(today), '%Y-%m-%d'))) - 1

# 今天开始时间戳
today_start_time = yesterday_end_time + 1

# 今天结束时间戳
today_end_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d'))) - 1

# 明天开始时间戳
tomorrow_start_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d')))

# 明天结束时间戳
tomorrow_end_time = int(time.mktime(time.strptime(str(acquire), '%Y-%m-%d'))) - 1

print '今天时间戳'
print today_start_time
print today_end_time
print '昨天时间戳'
print yesterday_start_time
print yesterday_end_time
print '明天时间戳'
print tomorrow_start_time
print tomorrow_end_time

获取当前日期时间,当前时间10位、13位时间戳

import time

#当前日期
t = time.strftime("%Y/%m/%d %H:%M:%S") # 2018-01-01 13:27:59

#当前时间10位时间戳 
t = time.time() # 1436428325.88
t = int(time.time()) # 1436428325

#当前时间13位时间戳
lambda: int(round(time.time() * 1000)) # 1378761833768

日期转10位时间戳

# 字符串类型时间,转为时间戳
def strTime_to_timeStamp(strtime):
    # 转为时间数组
    timeArray = time.strptime(strtime, "%Y/%m/%d %H:%M:%S")
    #转为时间戳
    timeStamp = int(time.mktime(timeArray)) 
    print(timeStamp) # 1515774430
    return  timeStamp 

10位时间戳转日期

def timeStamp_to_strTime(strtime):
    timeStamp = 1515774430
    strTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timeStamp))
    print(strTime) # 2018-01-01 13:27:59
    return strTime

发表评论

邮箱地址不会被公开。 必填项已用*标注