Python中用datetime包进行对时间的一些操作
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包进行对时间的一些操作的更多相关文章
- Python 调用datetime或者time获取时间的时候以及时间转换,最好设置一下时区 否则会出现相差8个小时的情况
在使用调用datetime或者time获取时间的时候以及时间转换,最好设置一下时区, 因为不同机器设置的时区不同,获取的时间可能就不对,正好我们使用的这两个服务器使用的都是东八区,所以没有问题,设置方 ...
- Python标准库02 时间与日期 (time, datetime包)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python具有良好的时间和日期管理功能.实际上,计算机只会维护一个挂钟时间(wa ...
- python学习笔记23(时间与日期 (time, datetime包))
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime. time包 time包基于C语言的库函数(library functions).Python的解释器通 ...
- (转)Python标准库02 时间与日期 (time, datetime包)
原文:http://www.cnblogs.com/vamei/archive/2012/09/03/2669426.html https://www.liaoxuefeng.com/wiki/001 ...
- Python学习笔记9:标准库之日期时间(time包,datetime包)
一 time包 sleep([float time]) 延迟一段以浮点数表示的秒数 time包基于C语言的库函数(library functions). Python的解释器一般是用C编写的,Pyth ...
- python--标准库 时间与日期 (time, datetime包)
Python具有良好的时间和日期管理功能.实际上,计算机只会维护一个挂钟时间(wall clock time),这个时间是从某个固定时间起点到现在的时间间隔.时间起点的选择与计算机相关,但一台计算机的 ...
- python 有关datetime时间日期 以及时间戳转换
直接上代码 其中有注释 #coding=utf-8 import time import datetime def yes_time(): #获取当前时间 now_time = datetime.da ...
- Python之扩展包安装
读者朋友,在比较新的版本(Python 2 >=2.7.9 or Python 3 >=3.4)中,pip或者easy_install 扩展包命令已经默认安装(可查看 你的安装目录\p ...
- 基于Python的datetime模块和time模块源码阅读分析
目录 1 前言 2 datetime.pyi源码分步解析 2.1 头部定义源码分析 2.2 tzinfo类源码分析 2.3 date类源码分析 2.4 time类源码分析 2.5 timedelta ...
随机推荐
- PgSQL · 特性分析 · 谈谈checkpoint的调度
在PG的众多参数中,参数checkpoint相关的几个参数颇为神秘.这些参数与checkpoint的调度有关,对系统的稳定性还是比较重要的,下面我们为大家解析一下,这要先从PG的数据同步机制谈起. P ...
- js中退出语句break,continue和return 比较
js中退出语句break,continue和return 比较 在 break,continue和return 三个关键字中, break,continue是一起的,return 是函数返回语句,但是 ...
- python中文输出和写入文本
中文输出 #-*-coding:utf8-*- import requests import re timeout = 8 headers = {'User-Agent':'Mozilla/5.0 ( ...
- C#4.0新特性:可选参数,命名参数,Dynamic
1.可选参数 可以为方法的参数设置一个默认值,如下: class Program { static void Main(string[] args) { Show(); Show("cary ...
- mov视频转gif
在mac上用quicktime录屏,得到一个mov视频,想转成gif,试了几个工具,感觉下面这个最好用: http://ezgif.com/video-to-gif 支持重新指定尺寸和帧率,转完gif ...
- 取消GridView/ListView item被点击时的效果
方法一,在控件被初始化的时候设置 ? 1 2 gridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); listView.setSelec ...
- jquery点击改变class并toggle
<html> <head> <meta charset="utf-8"> <title></title> <scr ...
- collection的框架结构
经常会看到程序中使用了记录集,常用的有Collection.HashMap.HashSet.ArrayList,因为分不清楚它们之间的关系,所以在使用时经常会混淆,以至于不知道从何下手.在这儿作了一个 ...
- HTML 图像
通过使用 HTML,可以在文档中显示图像. 实例 插入图像 本例演示如何在网页中显示图像. 从不同的位置插入图片 本例演示如何将其他文件夹或服务器的图片显示到网页中. (可以在本页底端找到更多实例.) ...
- 面向对象设计模式--策略模式Strategy
策略模式的UML类图(VS2013 C++版本): 策略模式的重点:每个策略对象封装一个算法,有多少个算法就有多少个对象.策略模式的意图是封装算法.要从“抽象不仅面对状态(字段.属性)还面对行为(算法 ...