1. 计算给出两个时间之间的时间差

import datetime as dt
# current time
cur_time = dt.datetime.today()
# one day
pre_time = dt.date(2016, 5, 20) # eg: 2016.5.20
delta = cur_time - pre_time
# if you want to get discrepancy in days
print delta.days
# if you want to get discrepancy in hours
print delta.hours
# and so on

2. 获取n天前的时间

cur_time = dt.now()
# previous n days
pre_time = dt.timedelta(days=n)

3. 将给定的时间精确到天或者其他单位

cur_time = dt.now()
# get day of current time
cur_day = cur_time.replace(hour=0, minute=0, second=0, mircrosecond=0)

4. 获取一连串的时间序列(返回list)

cur_time = dt.datetime.today()
datelist = [cur_time - dt.timedelta(days=x) for x in range(0, 100)]

或者

import pandas as pd
datelist = pd.date_range(pd.datetime.today(), periods=100).tolist()

5. 将时间字符串转化为datetime类型

date_formate = "%Y-%m-%d" # year-month-day
time = dt.strptime('2016-06-22', date_format)

6. 将时间类型转化为字符串类型

time_str = dt.strftime("%Y-%m-%d", dt.now()) # return like "2016-06-22"

参考:

https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

http://stackoverflow.com/questions/993358/creating-a-range-of-dates-in-python

http://stackoverflow.com/questions/151199/how-do-i-calculate-number-of-days-betwen-two-dates-using-python

http://stackoverflow.com/questions/3183707/stripping-off-the-seconds-in-datetime-python

Python中用datetime包进行对时间的一些操作的更多相关文章

  1. Python 调用datetime或者time获取时间的时候以及时间转换,最好设置一下时区 否则会出现相差8个小时的情况

    在使用调用datetime或者time获取时间的时候以及时间转换,最好设置一下时区, 因为不同机器设置的时区不同,获取的时间可能就不对,正好我们使用的这两个服务器使用的都是东八区,所以没有问题,设置方 ...

  2. Python标准库02 时间与日期 (time, datetime包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python具有良好的时间和日期管理功能.实际上,计算机只会维护一个挂钟时间(wa ...

  3. python学习笔记23(时间与日期 (time, datetime包))

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime. time包 time包基于C语言的库函数(library functions).Python的解释器通 ...

  4. (转)Python标准库02 时间与日期 (time, datetime包)

    原文:http://www.cnblogs.com/vamei/archive/2012/09/03/2669426.html https://www.liaoxuefeng.com/wiki/001 ...

  5. Python学习笔记9:标准库之日期时间(time包,datetime包)

    一 time包 sleep([float time]) 延迟一段以浮点数表示的秒数 time包基于C语言的库函数(library functions). Python的解释器一般是用C编写的,Pyth ...

  6. python--标准库 时间与日期 (time, datetime包)

    Python具有良好的时间和日期管理功能.实际上,计算机只会维护一个挂钟时间(wall clock time),这个时间是从某个固定时间起点到现在的时间间隔.时间起点的选择与计算机相关,但一台计算机的 ...

  7. python 有关datetime时间日期 以及时间戳转换

    直接上代码 其中有注释 #coding=utf-8 import time import datetime def yes_time(): #获取当前时间 now_time = datetime.da ...

  8. Python之扩展包安装

    读者朋友,在比较新的版本(Python 2 >=2.7.9 or Python 3 >=3.4)中,pip或者easy_install 扩展包命令已经默认安装(可查看   你的安装目录\p ...

  9. 基于Python的datetime模块和time模块源码阅读分析

    目录 1 前言  2 datetime.pyi源码分步解析 2.1 头部定义源码分析 2.2 tzinfo类源码分析 2.3 date类源码分析 2.4 time类源码分析 2.5 timedelta ...

随机推荐

  1. Uinty3d 镜面反射代码

    镜面反射代码 文件名MirrorReflection.cs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...

  2. 取得某个数组前key大 PHP实现

    <?php function max_key($arr, $key) { $tmp_key = $key; $max_arr = array(); while($tmp_key--) $max_ ...

  3. unity, polygon collider 2D 添加顶点

    正常情况下只要按下了Edit Collider按钮,鼠标停在polygon collider 2D的一条边上,就会出现一个虚拟的新顶点,此时如果按下鼠标,新顶点就创建出来了. 但是我今天遇到一个奇怪的 ...

  4. WebApiThrottle限流框架

    ASP.NET Web API Throttling handler is designed to control the rate of requests that clients can make ...

  5. Android: 在 TextView 里使用删除线

    Android: 在 TextView 里使用删除线 分类: Android2014-09-25 13:17 3431人阅读 评论(0) 收藏 举报 以编程的方式添给 TextView 添加删除线: ...

  6. typeof和instanceof运算符

    (1)typeof运算符用于判断某一个变量的数据类型,它可以作为函数来用,如typeof(a)可以返回变量a的数据类型:也可以作为一个运算符来使用,例如typeof a 也可以返回变量a的数据类型,不 ...

  7. Anaconda多环境多版本python配置指导

    Anaconda多环境多版本python配置指导 字数3696 阅读644 评论0 喜欢0 最近学python,读完了语法后在GitHub找了一些练习来做,由 于学的是python3.x语法,而Git ...

  8. Asianux的SSH登录问题,密码不正确解决

    第一.ssh服务默认是关闭的,需要手动打开 [root@Asianux ~]# service sshd start      启动 [root@Asianux ~]#chkconfig sshd - ...

  9. sql字符串查找大小写敏感相关

    select * from table   where target_text like "3"; 等价于 select * from table   where target_t ...

  10. The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine

    问题描述: 修改一个工具功能为读取excel文件中的数据(xls) 本机(windows server 2003 32位) 调试运行正常,部署到服务器(windows server 2003 64位) ...