Python实现根据日期获取当天凌晨时间戳的方法示例

yipeiwu_com5年前Python基础

本文实例讲述了Python实现根据日期获取当天凌晨时间戳的方法。分享给大家供大家参考,具体如下:

# -*- coding:utf-8 -*-
#! python2
'''
Created on 2019年4月9日
@author: Administrator
'''
import datetime
import time
def get_day_zero_time(date):
  """根据日期获取当天凌晨时间"""
  if not date:
    return 0
  date_zero = datetime.datetime.now().replace(year=date.year, month=date.month,
                         day=date.day, hour=0, minute=0, second=0)
  date_zero_time = int(time.mktime(date_zero.timetuple()))
  return date_zero_time
# 今天的日期
today_date = datetime.datetime.now().date()
# 今天的零点
today_zero_time = get_day_zero_time(today_date)
#print today_date
print today_zero_time

运行结果:

1554739200

PS:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:

在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在线万年历日历:
http://tools.jb51.net/bianmin/wannianli

在线阴历/阳历转换工具:
http://tools.jb51.net/bianmin/yinli2yangli

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python日期与时间操作技巧总结》、《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

python版大富翁源代码分享

本文实例为大家分享了python版大富翁游戏的具体代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- # code by: 物网141 王璞劼K...

python实现2048小游戏

python实现2048小游戏

2048的python实现。修改自某网友的代码,解决了原网友版本的两个小bug: 1. 原版游戏每次只消除一次,而不是递归消除。如 [2 ,2 ,2 ,2] 左移动的话应该是 [4,...

pandas 将索引值相加的方法

如下所示: s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) s2 = pd.Series([10, 20, 30...

python 限制函数执行时间,自己实现timeout的实例

如下所示: # coding=utf-8 import signal import time def set_timeout(num, callback): def wr...

基于django传递数据到后端的例子

最近遇到一个问题,前端表单我写了多个按钮,每个按钮通过for循环来给name赋值如下: <input type="button" class="btn btn-info btn...