datetime在python中比较常用,主要用来处理时间日期,使用前先倒入datetime模块。下面总结下本人想到的几个常用功能。

1、当前时间:

>>> print datetime.datetime.now()
2015-07-17 16:39:15.712000
>>> print type(datetime.datetime.now())
<type 'datetime.datetime'>
返回的datetime时间格式。

2、当前日期

>>> print datetime.datetime.now().date()
2015-07-17
>>> print type(datetime.datetime.now().date())
<type 'datetime.date'>

3、当前时间tuple

>>> datetime.datetime.now().timetuple()
time.struct_time(tm_year=2015, tm_mon=7, tm_mday=17, tm_hour=16, tm_min=51, tm_sec=26, tm_wday=4, tm_yday=198, tm_isdst=-1)
>>> datetime.datetime.now().timetuple().tm_mday
17

4、时间移动(几天、几小时前后...)

使用datetime.timedelta这个方法来前后移动时间,可以用的参数有weeks,days,hours,minutes,seconds,microseconds。

>>> print datetime.datetime.now() + datetime.timedelta(days=1)
2015-07-18 16:49:48.574000
>>> print datetime.datetime.now() + datetime.timedelta(hours=1)
2015-07-17 17:49:57.122000
>>> print datetime.datetime.now() + datetime.timedelta(minutes=-30)
2015-07-17 16:20:08.619000

上个月最后一天

>>> print datetime.date(day=1,month=datetime.date.today().month,year=datetime.date.today().year) - datetime.timedelta(days=1)
2015-06-30

5、获取两个时间的时间差

>>> (datetime.datetime.now() - datetime.datetime.utcnow()).total_seconds()
28800.0

6、时间转化

datetime转str格式:

>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2015-07-17 16:58:46'

str格式转datetime格式:

>>> datetime.datetime.strptime("2015-07-17 16:58:46","%Y-%m-%d %H:%M:%S")
datetime.datetime(2015, 7, 17, 16, 58, 46)
>>> print datetime.datetime.strptime("2015-07-17 16:58:46","%Y-%m-%d %H:%M:%S")
2015-07-17 16:58:46
>>> print type(datetime.datetime.strptime("2015-07-17 16:58:46","%Y-%m-%d %H:%M:%S"))
<type 'datetime.datetime'>

datetime转timestamp:

>>> import time
>>> now=datetime.datetime.now()
>>> time.mktime(now.timetuple())
1437123812.0

timestamp转datetime:

>>> datetime.datetime.fromtimestamp(1437123812.0)
datetime.datetime(2015, 7, 17, 17, 3, 32)
>>> print datetime.datetime.fromtimestamp(1437123812.0)
2015-07-17 17:03:32

Python中datetime的使用和常用时间处理的更多相关文章

  1. python中datetime模块中datetime对象的使用方法

    本文只讲述datetime模块中datetime对象的一些常用的方法,如果读者需要更多datetime模块的信息,请查阅此文档. datetime模块的对象有如下: timedelta date da ...

  2. python中datetime模块

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

  3. python中 datetime模块的详解(转载)

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

  4. Python中datetime库的用法

    datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime ...

  5. Pandas Timestamp 和 python 中 datetime 的互相转换

    Pandas 的Timestamp 和 python 的 datetime,   这是两种不同的类型. 它们之间可以互相转换. refer to: https://www.jianshu.com/p/ ...

  6. Python中生成器,迭代器,以及一些常用的内置函数.

    知识点总结 生成器 生成器的本质就是迭代器. 迭代器:Python中提供的已经写好的工具或者通过数据转化得来的. 生成器:需要我们自己用Python代码构建的 创建生成器的三种方法: 通过生成器函数 ...

  7. python中datetime模块中strftime/strptime函数

    f==format p==parse 1.获取当前时间(日期格式) from datetime import datetime datetime.now()#输出 datetime.datetime( ...

  8. python中datetime常用方法

    # 可运算的时间方法包 >>> import datetime >>> import time >>> res = datetime.dateti ...

  9. python 中datetime 和 string 转换

    dt = datetime.datetime.strptime(string_date, fmt) fmt 的格式说明如下: https://docs.python.org/2/library/dat ...

随机推荐

  1. Linux命令apt-get apt的常见用法

    高级包装工具(英语:Advanced Packaging Tools,缩写为APT) apt-cache search foo //搜索和"foo"匹配的包. apt-cache ...

  2. Mac Security工具使用总结find-identity

    Security是Mac系统中钥匙串和安全模块的命令行管理工具,(图形化工具为Keychain Access.app).钥匙串(Keychain)实质上就是一个用于存放证书.密钥.密码等安全认证实体的 ...

  3. 将到来的战略转变:移动 Web 还是移动 Apps?

    目前来看,移动应用比移动网站的易用性更高,但变化即将发生,移动网站最终将优于Apps,成为更好的策略选择. 一家公司制定移动策略时,最重要的问题是:是否需要考虑为移动设备特别做点什么.一些公司永远都不 ...

  4. mac上利用minikube搭建kubernetes(k8s)环境

    友情提示:对于初次接触k8s的同学,强烈建议先看看本文最后的参考文章. 环境: mac os(Mojave) 前提:先安装好kubectl (brew install kubectl) .docker ...

  5. WCF探索之旅(三)——IIS公布WCF服务

    在之前的博客已经对WCF的基础知识做了介绍.而且做了一个简单的小样例. 假设你看了之前的博客,相信你已经对WCF有了一定的掌握.这篇博客主要说一下怎样把WCF服务公布的IISserver上. 在上篇博 ...

  6. 【BZOJ3864】Hero meet devil DP套DP

    [BZOJ3864]Hero meet devil Description There is an old country and the king fell in love with a devil ...

  7. 【BZOJ4435】[Cerc2015]Juice Junctions Tarjan+hash

    [BZOJ4435][Cerc2015]Juice Junctions Description 你被雇佣升级一个旧果汁加工厂的橙汁运输系统.系统有管道和节点构成.每条管道都是双向的,且每条管道的流量都 ...

  8. 解决因 gtx 显卡而导致的 google chrome 颜色显示不正常。色彩变淡发白,其实很简单

    笔者因为换了用 gtx 1050 显卡替换了原来的集显. 导致chrome浏览器渲染颜色变淡而且泛白. 查了下肯能是因为换了显卡,没换高清显示器. 导致chrome自动启用了 dispaly p3 d ...

  9. 关于TransactionScope 使用

    在去年的项目中使用了TransactionScope,现在总结下TransactionScope的使用说明 一.TransactionScope是.Net Framework 2.0之后,新增了一个名 ...

  10. IIS发布问题集锦

    1. 2.文件都是Not Found 3.删除了PrecompiledApp.config文件就可以了: 4.预编译:http://blog.darkthread.net/post-2012-04-2 ...