一、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中时间操作总结的更多相关文章

  1. python中时间格式

    问题:通过MySQLdb查询datetime字段,然后通过浏览器显示出来,得到的格式是:         'Thu, 19 Feb 2009 16:00:07 GMT'   (http呈现出来的格式) ...

  2. Python常用时间操作总结【取得当前时间、时间函数、应用等】转载

    Python常用时间操作总结[取得当前时间.时间函数.应用等] 转载  2017-05-11   作者:清风乐逍遥    我要评论 这篇文章主要介绍了Python常用时间操作,包括取得当前时间.时间函 ...

  3. python中时间的基本使用

    格式化日期 我们可以使用 time 模块的 strftime 方法来格式化日期,: time.strftime(format[, t]) #!/usr/bin/python # -*- coding: ...

  4. python中时间日期格式化符号

    python中时间日期格式化符号: import time print(time.strftime('%Y%H%M%S', time.localtime())) 运行结果: 2016092308 %y ...

  5. python中文件操作的六种模式及对文件某一行进行修改的方法

    一.python中文件操作的六种模式分为:r,w,a,r+,w+,a+ r叫做只读模式,只可以读取,不可以写入 w叫做写入模式,只可以写入,不可以读取 a叫做追加写入模式,只可以在末尾追加内容,不可以 ...

  6. python中文件操作的其他方法

    前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...

  7. Neo4j:图数据库GraphDB(四)Python中的操作

    本文总结下Python中如何操作Neo4j数据库,用到py2neo包,Pip install 一下. 1 连接neo4j数据库:跟其它数据库一样,操作前必须输入用户名和密码及地址连接一下. from ...

  8. python MySQLdb用法,python中cursor操作数据库(转)

    数据库连接 连接数据库前,请先确认以下事项: 您已经创建了数据库 TESTDB. 在TESTDB数据库中您已经创建了表 EMPLOYEE EMPLOYEE表字段为 FIRST_NAME, LAST_N ...

  9. python中时间的转换和使用datetime

    模块 一个完整大型的python程序是由模块和包的形式组织起来的,可见模块在python中的重要性.模块是一种组织型式,它许多有关联(关系)的代码组织放到单独的独立文件中.简单的说,可以把模块理解为一 ...

随机推荐

  1. 请教中文的OCR软件哪个最好?

    对原件,有这几种:1,全中文(含标点,一些英文符号)2,中英文(英文较多,或英文占大多数)3,全英文(这个用finereader8.0) 針對1.2 紫光 超星阅读器自带的OCR组件 尚书七号搞定全中 ...

  2. Linux下的ip命令

    linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者.使用ip命令,只需一个命令,你就能很轻松地执行一些网络管理任务.ifconfig是net-tools中已被废弃使用的一个命 ...

  3. android http post

    public static boolean postInfo(String info1, String info2, String info3, ....) { final HttpParams ht ...

  4. QWidget子窗口中setStyleSheet无效,解决方法

    继承 QWidget setStyleSheet无效,解决方法. 发现 继承自QWidget的自定义类 ,使用setStyleSheet无效, 如果删除头文件中的 Q_OBJECT,setStyleS ...

  5. FPGA Prototyping By Verilog Examples第五章 状态机FSM设计

    上升沿检测电路之Moore型FSM // Listing 5.3module edge_detect_moore ( input wire clk, reset, input wire level, ...

  6. 【Android】3.12 兴趣点( POI)搜索功能

    分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 POI(Point of Interest),中文可以翻译为“兴趣点”.在地理信息系统中,一个POI ...

  7. Untrusted 游戏 通关攻略

    这个游戏还不错,用了两个晚上的时间通过并写下解法.这个游戏通过修改JS代码来通关的游戏.很考验玩家的解决问题能力,同时也有一定的可玩性. 游戏地址 http://alexnisnevich.githu ...

  8. 428. Pow(x, n)【medium】

    Implement pow(x, n). Notice You don't need to care about the precision of your answer, it's acceptab ...

  9. 【转】PowerDesigner表结构和字段大小写转换

    [转自]http://blog.csdn.net/xysh1991/article/details/8016192 使用方法:进入PowerDesigner,打开一个PDM,在菜单栏找到:Tools ...

  10. Android开发日记(一)

    实现点击一个图片按钮跳转到一个动作ImageViewSchoolCard = (ImageView) view.findViewById(R.id.ImageViewLostThings); Imag ...