python 有关datetime时间日期 以及时间戳转换
直接上代码 其中有注释
#coding=utf-8
import time
import datetime def yes_time():
#获取当前时间
now_time = datetime.datetime.now()
#当前时间减去一天 获得昨天当前时间
yes_time = now_time + datetime.timedelta(days=-1)
#格式化输出
yes_time_str = yes_time.strftime('%Y-%m-%d %H:%M:%S')
print yes_time_str # 2017-11-01 22:56:02 def dif_time():
#计算两个时间之间差值
now_time = datetime.datetime.now()
now_time = now_time.strftime('%Y-%m-%d %H:%M:%S')
d1 = datetime.datetime.strptime('2017-10-16 19:21:22', '%Y-%m-%d %H:%M:%S')
d2 = datetime.datetime.strptime(now_time, '%Y-%m-%d %H:%M:%S')
#间隔天数
day = (d2 - d1).days
#间隔秒数
second = (d2 - d1).seconds
print day #
print second #13475 注意这样计算出的秒数只有小时之后的计算额 也就是不包含天之间差数 def unix_time():
#将python的datetime转换为unix时间戳
dtime = datetime.datetime.now()
un_time = time.mktime(dtime.timetuple())
print un_time #1509636609.0
#将unix时间戳转换为python 的datetime
unix_ts = 1509636585.0
times = datetime.datetime.fromtimestamp(unix_ts)
print times #2017-11-02 23:29:45
python 有关datetime时间日期 以及时间戳转换的更多相关文章
- python datetime 时间日期处理小结
python datetime 时间日期处理小结 转载请注明出处:http://hi.baidu.com/leejun_2005/blog/item/47f340f1a85b5cb3a50f5232. ...
- python中将datetime对象转化为时间戳
从mongodb中读取出来的记录中,时间存储在datetime对象里,返回给客户端的却要求是时间戳格式,因此需要将对应的datetime时间转化为时间戳,从stackoverflow上找到同样的问题和 ...
- PHP使用DateTime类做时间日期到字符串转换
PHP关于时间日期的处理不是很规范,简单就简单了,就是不知道输入的字符串是否能够正确转化为需要的DateTime类型. 面向对象的PHP应该使用DateTime类来做string和dateTime的转 ...
- js 时间日期与时间戳之间转换
1 1.将时间(2017-08-10)转换时间戳 2 var startTime = '2017-08-10'; 3 var startdate = new Date(Date.parse(start ...
- Python入门 —— 05时间日期处理小结
此文多涉及基础,如果想要深入理解则到文末,有提供链接 涉及对象 1. datetime 2. timestamp 3. time tuple 4. string 5. date - datetime基 ...
- Python中对时间日期的处理方法简单汇总
这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...
- python time模块介绍(日期格式化 时间戳)
import time # 1.time.time() 用于获取当前时间的时间戳, ticks = time.time() print(ticks) # 1545617668.8195682 浮点数 ...
- 解决 python 中,时间日期不能序列化的问题
在python 中, 你在数据库娶到了数据中如果含有时间日期,那么你在向前端作为json对象传递的时候呢,就会报错.大致如下: TypeError: datetime.datetime(2017, 1 ...
- Python获取当前时间或者当前时间戳【转】
取得时间相关的信息的话,要用到python time模块,python time模块里面有很多非常好用的功能,你可以去官方文档了解下,要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年 ...
随机推荐
- js根据时间戳倒计时
今天有个需求,要在页面上做当前时间距离下个月1号的倒计时.在网上找了很多案例也试了很多,大部分都是获取本地当前时间,然后设置结束时间进行计算,然后倒计时.但是有几个问题: 1.如果本地时间和服务器时间 ...
- 标准输入输出 stdio 流缓冲
**From : http://www.pixelbeat.org/programming/stdio_buffering/** 我发现找出标准流用的是什么缓冲是一件困难的事. 例如下面这个使用uni ...
- go基础编程 day-1
Go语言的特性 开启了学习新的语言路程,记录每天学习的笔记,与大家一起分享. ①.自动垃圾回收 ②.更丰富的内置类型 ③.函数多返回值 ④.错误处理 ⑤.匿名函数和闭包 ⑥.类型和接口 ⑦.并发编程 ...
- 恶意软件Mirai换了个马甲 瞄上我国2亿多台IoT设备
恶意软件Mirai换了个马甲 瞄上我国2亿多台IoT设备 想要起来时,一种沉重感阻碍着他,这是一种安全感:感觉到一张床为他铺好了,而且只属于他:想要静卧时,一种不安阻碍着他,把他从床上赶起来,这是 ...
- php 守护进程类
最近个人项目中需要后台运行任务,之前一直是用nouhp & + 重定向输出 来后台跑任务,后来觉得不好维护原始数据,同时可能也没有直接操作进程那么稳吧(没验证).废话少说,来看分析. 首先,我 ...
- Spring 组成
Spring 主要由 Bean Context Core 三个组件组成 Bean 组件是整个 Spring 核心,Spring 通过管理 Bean 组件提供服务 Context 是 Bean 组件容器 ...
- PHP设计模式四:适配器模式
一.什么是适配器模式 适配器模式有两种:类适配器模式和对象适配器模式.其中类适配器模式使用继承方式,而对象适配器模式使用组合方式.由于类适配器 模式包含双重继承,而PHP并不支持双重继承,所以一般都采 ...
- LeetCode 560. Subarray Sum Equals K (子数组之和等于K)
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- LeetCode 226. Invert Binary Tree (反转二叉树)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- LeetCode 189. Rotate Array (旋转数组)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...