昨天的时间戳python

时间:2016-11-18 08:15:39

标签: python linux unix-timestamp

我想得到昨天linux时间戳的开始时间和结束时间

import time
startDay = time.strftime('%Y-%m-%d 00:00:00')
print startDay
endDay   =time.strftime('%Y-%m-%d 23:59:59')
print endDay
  

输出是:

     

2016-11-18 00:00:00

     

2016-11-18 23:59:59

今天在字符串中显示开始时间和结束时间 我希望得到昨天的linux时间戳的开始时间和结束时间 像:

  

4319395200

     

4319481599

4 个答案:

答案 0 :(得分:2)

import time
def datetime_timestamp(dt):
    time.strptime(dt, '%Y-%m-%d %H:%M:%S')
    s = time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S'))
    return int(s)

答案 1 :(得分:0)

from datetime import datetime, date, time, timedelta

# get start of today
dt = datetime.combine(date.today(), time(0, 0, 0))
# start of yesterday = one day before start of today
sday_timestamp = int((dt - timedelta(days=1)).timestamp())
# end of yesterday = one second before start of today
eday_timestamp = int((dt - timedelta(seconds=1)).timestamp())

print(sday_timestamp)
print(eday_timestamp)

或者:

# get timestamp of start of today
dt_timestamp = int(datetime.combine(date.today(), time(0, 0, 0)).timestamp())
# start of yesterday = start of today - 86400 seconds
sday_timestamp = dt_timestamp - 86400
# end of yesterday = start of today - 1 second
eday_timestamp = dt_timestamp - 1

答案 2 :(得分:0)

使用perl命令的强大功能,无需导入时间。

Startday = $(perl -e'使用POSIX; print strftime“%Y - % - m-%d 00:00:00”,localtime time-86400;') 结束日= $(perl -e'使用POSIX;打印strftime“%Y - % - m-%d 23:59:59”,本地时间-86400;')

echo $ Startday echo $ Endday

startday指定= date --date='1 day ago' +%Y%m%d\t00:00:00 startday指定= date --date='1 day ago' +%Y%m%d\t23:59:59

echo $ Startday echo $ Endday

答案 3 :(得分:0)

import datetime
midnight2 = datetime.datetime.now().replace(hour=0,minute=0,second=0, microsecond=0)
midnight2 = midnight2 - datetime.timedelta(seconds= +1)
midnight1 = midnight2 - datetime.timedelta(days= +1, seconds= -1)
base = datetime.datetime.fromtimestamp(0)
yesterday = (midnight1 - base).total_seconds()
thismorning = (midnight2 - base).total_seconds()
print midnight1,"timestamp",int(yesterday)
print midnight2,"timestamp",int(thismorning)
print "Seconds elapsed",thismorning - yesterday

截至2016年11月18日的结果:

2016-11-17 00:00:00 timestamp 1479337200
2016-11-17 23:59:59 timestamp 1479423599
Seconds elapsed 86399.0