python中时间操作总结
一、time

二、datetime
1.获取当前系统时间
datenow = datetime.datetime.now()
2.将datetime格式的时间转换成str
datenow = datenow.strftime("%Y-%m-%d")
或datenow = datetime.datetime.strftime(datenow, "%Y-%m-%d")
3.将str格式的时间转换成datetime格式
datenow = datetime.datetime.strptime(datenow, "%Y-%m-%d")
4.已知datetime格式的一个时间,向前取某一历史的日期
import calendar
①年
for i in range(1,n):
his_year = datenow.year - i
his_mon = datenow.month
his_day = min(datenow.day, calendar.monthrange(his_year,his_mon)[1]) #天 不能超过当月的 最大天数
his_date = datetime.datetime(his_year, his_mon, his_day)
②月
for i in range(1,n):
month = datenow.month -1 -i
his_year = datenow.year + month/12
his_mon = month%12 + 1
his_day = min(datenow.day, calendar.monthrange(his_year,his_mon)[1])
his_date = datetime.datetime(his_year, his_mon, his_day)
③天
for i in range(1,n):
his_date = datenow - datetime.timedelta(days=i)
5.已知datetime格式的一个日期,获取是星期几(weekday() )
datenow = datetime.datetime.now()
weekday = datenow.weekday() #返回0-6
python中时间操作总结的更多相关文章
- python中时间格式
问题:通过MySQLdb查询datetime字段,然后通过浏览器显示出来,得到的格式是: 'Thu, 19 Feb 2009 16:00:07 GMT' (http呈现出来的格式) ...
- Python常用时间操作总结【取得当前时间、时间函数、应用等】转载
Python常用时间操作总结[取得当前时间.时间函数.应用等] 转载 2017-05-11 作者:清风乐逍遥 我要评论 这篇文章主要介绍了Python常用时间操作,包括取得当前时间.时间函 ...
- python中时间的基本使用
格式化日期 我们可以使用 time 模块的 strftime 方法来格式化日期,: time.strftime(format[, t]) #!/usr/bin/python # -*- coding: ...
- python中时间日期格式化符号
python中时间日期格式化符号: import time print(time.strftime('%Y%H%M%S', time.localtime())) 运行结果: 2016092308 %y ...
- python中文件操作的六种模式及对文件某一行进行修改的方法
一.python中文件操作的六种模式分为:r,w,a,r+,w+,a+ r叫做只读模式,只可以读取,不可以写入 w叫做写入模式,只可以写入,不可以读取 a叫做追加写入模式,只可以在末尾追加内容,不可以 ...
- python中文件操作的其他方法
前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...
- Neo4j:图数据库GraphDB(四)Python中的操作
本文总结下Python中如何操作Neo4j数据库,用到py2neo包,Pip install 一下. 1 连接neo4j数据库:跟其它数据库一样,操作前必须输入用户名和密码及地址连接一下. from ...
- python MySQLdb用法,python中cursor操作数据库(转)
数据库连接 连接数据库前,请先确认以下事项: 您已经创建了数据库 TESTDB. 在TESTDB数据库中您已经创建了表 EMPLOYEE EMPLOYEE表字段为 FIRST_NAME, LAST_N ...
- python中时间的转换和使用datetime
模块 一个完整大型的python程序是由模块和包的形式组织起来的,可见模块在python中的重要性.模块是一种组织型式,它许多有关联(关系)的代码组织放到单独的独立文件中.简单的说,可以把模块理解为一 ...
随机推荐
- spring boot几个初始配置文件
一.gradle // https://mvnrepository.com/artifact/com.squareup.okhttp/okhttp compile group: 'com.square ...
- Objective-C中的@Property具体解释
Objective-C中的@Property具体解释 @Property (属性) class vairs 这个属性有nonatomic. strong. weak, retain. copy等等 我 ...
- Android开发14——监听内容提供者ContentProvider的数据变化
一.提出需求 有A,B,C三个应用,B中的数据需要被共享,所以B中定义了内容提供者ContentProvider:A应用修改了B应用的数据,插入了一条数据.有这样一个需求,此时C应用需要得到数据被修改 ...
- SQL SERVER树形结构数据——批量删除分组数据
定义函数获取某结点下所有子结点: CREATE FUNCTION [dbo].[fn_GetSubGroupInfoById] ( @id AS INT --某分组Id ) RETURNS @SubG ...
- httpwebrequest异步参考
http://www.cnblogs.com/SanMaoSpace/archive/2011/07/27/2118133.html http://www.cnblogs.com/qianlifeng ...
- Oracle PLSQL Demo - 09.Open、Fetch遍历游标[Open, Fetch, Close Record CURSOR]
declare r_emp scott.emp%rowtype; cursor cur_emp is select t.* from scott.emp t; begin open cur_emp; ...
- (转)iPhone开发关于UDID和UUID的一些理解
转自:http://www.cocoachina.com/bbs/read.php?tid=92404另外配合参考这里:https://github.com/ymsheng/ios-deviceUni ...
- hadoop输出lzo文件并添加索引
public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); ...
- eclipse手动build整个project
eclipse默认是自动build你所编辑的java文件,但是这种自动build的前提是你对该java文件做了修改,因此,有的时候,如果你的class文件因为某种原因丢失了,你又不去手动build,e ...
- PowerShell实现基于SharePoint的网站HomePage Auto-Configure Solution
Home Page Web Parts Auto-Configuration PS:该项目为公司项目,我还是给他的名字屏蔽掉吧,这是我用PowerShell写的一个自动化升级工具,此为三部自动化工具的 ...