python日期时间处理
time模块
#-*- coding: utf-8 -*- """
#获取当前时间的时间戳(单位秒)
time.time() #推迟指定秒数后再运行
time.sleep(N) #将时间元组转换为时间戳
time.mktime(t) #将时间戳转换为UTC+0时区的时间元组,默认转换time.time()
time.gmtime() #将时间戳转换为当地时区的时间元组,默认转换time.time()
time.localtime() #返回指定格式的时间格式
time.strftime(format[, t]) #将对应格式的时间字符串转换成时间元组
time.strptime(string[, format])
""" import time #时间字符串-->时间戳
time_tuple = time.strptime("2014-09-10 22:21:09", "%Y-%m-%d %H:%M:%S")
print time.mktime(time_tuple) #1410358869.0 #时间戳-->时间字符串
time_tuple = time.localtime(1410358869.0)
print time.strftime("%Y-%m-%d %H:%M:%S", time_tuple) #2014-09-10 22:21:09
datetime模块
#-*- coding: utf-8 -*- """
#时间对象
time_obj = datetime.time(时, 分, 秒)
time_obj.hour
time_obj.minute
time_obj.second
time_obj.microsecond #日期对象
date_obj = datetime.date(年, 月, 日)
date_obj.year
date_obj.month
date_obj.day #将日期对象、时间对象结合起来
datetime.datetime.combine(date,time) #当前日期的日期对象
datetime.date.today() #当前日期时间的日期时间对象
datetime.datetime.now()
datetime.datetime.today() #日期时间对象转换为时间元组
date_time_obj = datetime.datetime.now()
date_time_obj.timetuple() #将时间戳转换为当地时区的日期时间对象
datetime.datetime.fromtimestamp(timestamp) #将时间戳转换为UTC+0时区的日期时间对象
datetime.datetime.utcfromtimestamp(timestamp) #日期时间运算
datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]) #当前日期时间减去8小时
datetime.datetime.now() - datetime.timedelta(hours=8) #返回指定格式的时间格式
datetime.datetime.strftime(format) ##将对应格式的时间字符串转换为日期时间对象
datetime.datetime.strptime(string, format) """ import datetime
import time #时间字符串-->时间戳
date_time_obj = datetime.datetime.strptime("2014-09-10 22:21:09", "%Y-%m-%d %H:%M:%S")
print time.mktime(date_time_obj.timetuple()) #1410358869.0 #时间戳-->时间字符串
date_time_obj = datetime.datetime.fromtimestamp(1410358869.0)
print date_time_obj.strftime("%Y-%m-%d %H:%M:%S") #2014-09-10 22:21:09 #一个日期时间加上或者减去一个时间
date_time_obj2 = date_time_obj - datetime.timedelta(days=8,hours=2) print str(date_time_obj2) #2014-09-02 20:21:09
print date_time_obj2.strftime("%Y-%m-%d %H:%M:%S") #2014-09-02 20:21:09 #比较两个日期时间
print date_time_obj > date_time_obj2 #True #计算两个日期时间相差几天,忽略时间,只比较日期
print (date_time_obj-date_time_obj2).days #
python日期时间处理的更多相关文章
- Python日期时间函数处理
所有日期.时间的 api 都在datetime模块内. 1 日期的格式化输出 datetime => string import datetime now = datetime.datetime ...
- Python 日期时间处理模块学习笔记
来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...
- python 日期 & 时间
1. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 2. 时间间隔是以秒为单位的浮点小数. 3. 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长 ...
- Python日期时间Date/Time
Python程序可以处理多种方式的日期和时间.日期格式之间的转换是一种常见计算机的杂活. Python的时间和日历模块,能帮助处理日期和时间. Tick是什么? 为时间间隔,以秒为单位的浮点数.从“新 ...
- 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...
- 1、Python 日期时间格式化输出
今天帮朋友写自动化脚本,又需要用格式化日期,又忘记怎么写了,还是写到自己博客里面,方便日后需要的时候看一眼吧.So,临时加一篇 Python 的文章. 1.Python的time模块 import t ...
- python 日期时间处理
# 获取日期: import datetime #调用事件模块 today =datetime.date.today() #获取今天日期 deltadays =datetime.timedelta(d ...
- Python日期时间的相关操作
1.获取当前时间戳 import time t=time.time() print t 1459994552.51 #以秒为单位的 2.格式化日期 time.localtime() 返回当前时间的: ...
- Python日期时间函数
所有日期.时间的api都在datetime模块内. 1. 日期输出格式化 datetime => string import datetime now = datetime.datetime.n ...
随机推荐
- c#反序列化
C#序列化(转载) 2011-03-15 | 凯之风 | 转藏(2) 序列化有下列要求: A> 只序列化PUBLIC的成员变量和属性 B> 类必须有默认识构造函数 C> 如果 ...
- [经典] 最X(长 | 大和 | 大积)Y(子序列 | 子字符串)
Note: 子序列,可以不连续:子字符串,必须连续. 以下题目按在我看看来的难度从易到难排列: 最大和子序列(Maximum sum subsequence) 这道题纯属娱乐...应该不会有人出这种题 ...
- poj2318 水题(二分+叉积)
题目链接:http://poj.org/problem?id=2318 #include<cstdio> #include<cstring> #include<cmath ...
- poj 1269 水题
题目链接:http://poj.org/problem?id=1269 #include<cstdio> #include<cstring> #include<cmath ...
- Linux 环境变量 $PATH
我们知道查阅文件属性的指令 ls 完整文件名为:/bin/ls(这是绝对路径),那为什么可以在任何地方执行/bin/ls 这个指令呢? 为什么在任何目录下输入 ls 就一定可以显示出一些讯息而不会说 ...
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 来自投资银行的20个Java面试题
问题一:在多线程环境中使用HashMap会有什么问题?在什么情况下使用get()方法会产生无限循环? HashMap本身没有什么问题,有没有问题取决于你是如何使用它的.比如,你在一个线程里初始化了一个 ...
- bzoj4028: [HEOI2015]公约数数列
Description 设计一个数据结构. 给定一个正整数数列 a_0, a_1, ..., a_{n - 1},你需要支持以下两种操作: 1. MODIFY id x: 将 a_{id} 修改为 x ...
- 雅虎NCP:网络黄页的终结者
雅虎NCP是什么,NCP能做什么,为什么NCP能够成为网络黄页的终结者.NCP在颠覆既有市场格局的同时,其真实目的时什么?是成为网络化操作系统还是图谋最大化长尾广告.笔者相信,过不了多久,市场将会告诉 ...
- [Flux] Component / Views
The application will dislay a some catalogs, and each catalog has title image, description. Catalog: ...