Python中datetime的使用和常用时间处理
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的使用和常用时间处理的更多相关文章
- python中datetime模块中datetime对象的使用方法
本文只讲述datetime模块中datetime对象的一些常用的方法,如果读者需要更多datetime模块的信息,请查阅此文档. datetime模块的对象有如下: timedelta date da ...
- python中datetime模块
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...
- python中 datetime模块的详解(转载)
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...
- Python中datetime库的用法
datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime ...
- Pandas Timestamp 和 python 中 datetime 的互相转换
Pandas 的Timestamp 和 python 的 datetime, 这是两种不同的类型. 它们之间可以互相转换. refer to: https://www.jianshu.com/p/ ...
- Python中生成器,迭代器,以及一些常用的内置函数.
知识点总结 生成器 生成器的本质就是迭代器. 迭代器:Python中提供的已经写好的工具或者通过数据转化得来的. 生成器:需要我们自己用Python代码构建的 创建生成器的三种方法: 通过生成器函数 ...
- python中datetime模块中strftime/strptime函数
f==format p==parse 1.获取当前时间(日期格式) from datetime import datetime datetime.now()#输出 datetime.datetime( ...
- python中datetime常用方法
# 可运算的时间方法包 >>> import datetime >>> import time >>> res = datetime.dateti ...
- python 中datetime 和 string 转换
dt = datetime.datetime.strptime(string_date, fmt) fmt 的格式说明如下: https://docs.python.org/2/library/dat ...
随机推荐
- erlang中通过ip和子网掩码,计算地址范围 【二进制和十进制的转换】
在程序中,难免用的二进制和十进制之间的转换.遇到一个场景,通过ip和子网掩码,计算地址范围. 而地址范围为:网络地址+1—— 广播地址 -1 . 网络地址即ip和子网掩码的与的位运算.广播地址为:网 ...
- vagrant 介绍,安装与使用
可以帮你统一团队成员的开发环境.如果你或者你的伙伴创建了一个Vagrantfile,那么你只需要执行vagrant up就行了,所有的软件都会安装并且配置好.团队成员可以通过相同的Vagrantfil ...
- c++ 系统函数实现文件拷贝
#include "stdafx.h" #include <string> #include<windows.h> #include<iostream ...
- python 基础 1.5 python数据类型(三)--元组
一.python的数据类型 序列--元组 1.序列 1>字符串,列表,元组都是序列. 2>序列的两个特点是索引操作符和切片操作符 索引操作符:我们可以从序列中抓取一个特定项目 切片操作符: ...
- 修改linux的hostname (修改linux系统的IP和hostname)
# vi /etc/sysconfig/networkNETWORKING=yesHOSTNAME=yourname //在这修改hostnameNISDOMAIN=eng-cn.platform.c ...
- varnish代理缓存服务器的安装与使用
1. 下载解压 cd /usr/local/src/ wget https://codeload.github.com/varnishcache/varnish-cache/zip/master ch ...
- 利用wrapper注冊的 java服务起动超时的解决方法
使用Java Service Wrapper能够把java程序注冊为一个系统服务,可是在启动服务的某些情况下会抛出例如以下错误: ERROR | wrapper | 2014/06/11 08:4 ...
- Linux入门基础(二)——cp、nano命令
- ABAP 给动态变量赋值
[转自 http://blog.csdn.net/forever_crazy/article/details/6544830] 需求: 有时写程序的时候,需要给某个动态变量 赋值操作,当字段比较多时, ...
- Raspberry Pi3 ~ 使用eclipse进行远程调试
为了开发方便需要在电脑上对树莓派进行远程Debug. l 在eclipse中安装交叉编译(参照开发环境搭建) arm-linux-gnueabihf-gcc l 树莓派中检查是否安装了gdb ...