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

yipeiwu_com6年前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高级特性——详解多维数组切片(Slice)

(1) 我们先用arange函数创建一个数组并改变其维度,使之变成一个三维数组: >>> a = np.arange(24).reshape(2,3,4) >...

numpy实现合并多维矩阵、list的扩展方法

一、合并多个numpy矩阵 1、首先创建两个多维矩阵 矩阵a的大小为(2,3,2) 矩阵b的大小为(3,2,3) 采用concatentate这个函数就可以合并两个多维矩阵 合并之后...

Python中的pathlib.Path为什么不继承str详解

起步 既然所有路径都可以表示为字符串,为什么 pathlib.Path 不继承 str ? 这个想法的提出在 https://mail.python.org/pipermail...

python3.6 +tkinter GUI编程 实现界面化的文本处理工具(推荐)

python3.6 +tkinter GUI编程 实现界面化的文本处理工具(推荐)

更新: 2017.07.17  补充滚动条、win批处理拉起py 2017.08.13  新增自定义图标 一、背景: 1.工作中自己及同事在查数据库、测试接口时需要对一些字符串或json...

Python 中@property的用法详解

在绑定属性时,如果我们直接把属性赋值给对象,比如: p = Person() p.name= 'Mary' 我们先看个详细的例子(注意双下划线name和age定义为私有变量):...