Python时间获取及转换知识汇总
时间处理是我们日常开发中最最常见的需求,例如:获取当前datetime、获取当天date、获取明天/前N天、获取当天开始和结束时间(00:00:00 23:59:59)、获取两个datetime的时间差、获取本周/本月/上月最后一天等。而这些转换看起来很乱不容易记住,那么今天我们就来总结一下python的时间的处理。
原则:以datetime为中心, 起点或中转, 转化为目标对象, 涵盖了大多数业务场景中需要的日期转换处理
步骤:
1. 掌握几种对象及其关系
2. 了解每类对象的基本操作方法
3. 通过转化关系转化
datetime是date与time的结合体,包括date与time的所有信息。
函数原型是:
datetime. datetime (year, month, day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] )
各参数的含义与date、time的构造函数中的一样,要注意参数值的范围。
实例:
1. 获取datetime对象
代码如下:
| 
 1 
2 
3 
4 
5 
 | 
>>> import datetime>>> now = datetime.datetime.now()>>> nowdatetime.datetime(2016, 11, 26, 8, 34, 30, 876359)>>> type(now) | 
2. 获取timestamp(时间戳)
时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)以来的秒数。它也被称为 Unix 时间戳(Unix Timestamp)。
代码如下:
| 
 1 
2 
3 
 | 
>>> import time>>> time.time()1480120686.733905 | 
3. 获取time tuple (元组)
代码如下:
| 
 1 
2 
3 
 | 
>>> import time>>> time.localtime()time.struct_time(tm_year=2016, tm_mon=11, tm_mday=26, tm_hour=8, tm_min=39, tm_sec=33, tm_wday=5, tm_yday=331, tm_isdst=0) | 
4. 获取时间string(字符串)
字符串格式化参数列表:
datetime. strftime (format)
%a: 星期的简写。如 星期三为Web
%A: 星期的全写。如 星期三为Wednesday
%b: 月份的简写。如4月份为Apr
%B: 月份的全写。如4月份为April
%c: 日期时间的字符串表示。(如: 04/07/10 10:43:39)
%d: 日在这个月中的天数(是这个月的第几天)
%f: 微秒(范围[0,999999])
%H: 小时(24小时制,[0, 23])
%I: 小时(12小时制,[0, 11])
%j: 日在年中的天数 [001,366](是当年的第几天)
%m: 月份([01,12])
%M: 分钟([00,59])
%p: AM或者PM
%S: 秒(范围为[00,61],为什么不是[00, 59],参考python手册~_~)
%U: 周在当年的周数当年的第几周),星期天作为周的第一天
%w: 今天在这周的天数,范围为[0, 6],6表示星期天
%W: 周在当年的周数(是当年的第几周),星期一作为周的第一天
%x: 日期字符串(如:04/07/10)
%X: 时间字符串(如:10:43:39)
%y: 2个数字表示的年份
%Y: 4个数字表示的年份
%z: 与utc时间的间隔 (如果是本地时间,返回空字符串)
%Z: 时区名称(如果是本地时间,返回空字符串)
%%: %% => %
代码如下:
| 
 1 
2 
3 
 | 
>>> import datetime>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")'2016-11-26 08:40:39' | 
5. date(日期)
代码如下:
| 
 1 
2 
3 
 | 
>>> import datetime>>> datetime.datetime.now().date()datetime.date(2016, 11, 26) | 
6. 获取当天date
代码如下:
| 
 1 
2 
3 
 | 
>>> import datetime>>> datetime.date.today()datetime.date(2016, 11, 26) | 
7. 获取明天/前N天
明天
代码如下:
| 
 1 
2 
3 
 | 
>>> import datetime>>> datetime.date.today() + datetime.timedelta(days=1)datetime.date(2016, 11, 27) | 
两天前
代码如下:
| 
 1 
2 
3 
4 
5 
 | 
>>> import datetime>>> datetime.datetime.now()datetime.datetime(2016, 11, 26, 8, 42, 59, 665368)>>> datetime.datetime.now() - datetime.timedelta(days=3)datetime.datetime(2016, 11, 24, 8, 43, 14, 696948) | 
8. 获取当天开始和结束时间(00:00:00 23:59:59)
代码如下:
| 
 1 
2 
3 
4 
5 
 | 
>>> import datetime>>> datetime.datetime.combine(datetime.date.today(), datetime.time.min)datetime.datetime(2016, 11, 26, 0, 0)>>> datetime.datetime.combine(datetime.date.today(), datetime.time.max)datetime.datetime(2016, 11, 26, 23, 59, 59, 999999) | 
9. 获取两个datetime的时间差
代码如下:
| 
 1 
2 
3 
 | 
>>> import datetime>>> (datetime.datetime(2016,12,13,12,0,0) - datetime.datetime.now()).total_seconds()  1480506.809658 | 
10. 获取本周/本月/上月最后一天
本周
代码如下:
| 
 1 
2 
3 
4 
5 
6 
7 
 | 
>>> import datetime>>> today = datetime.date.today()>>> todaydatetime.date(2016, 11, 26)>>> sunday = today + datetime.timedelta(6 - today.weekday())>>> sundaydatetime.date(2016, 11, 27) | 
本月
代码如下:
| 
 1 
2 
3 
4 
5 
6 
 | 
>>> import calendar>>> today = datetime.date.today()>>> _, last_day_num = calendar.monthrange(today.year, today.month)>>> last_day = datetime.date(today.year, today.month, last_day_num)>>> last_daydatetime.date(2016, 11, 30) | 
获取上个月的最后一天(可能跨年)
代码如下:
| 
 1 
2 
3 
4 
5 
6 
 | 
>>> import datetime>>> today = datetime.date.today()>>> first = datetime.date(day=1, month=today.month, year=today.year)>>> lastMonth = first - datetime.timedelta(days=1)>>> lastMonthdatetime.date(2016, 10, 31) | 
其他使用例子:
| 
 1 
2 
3 
 | 
#当月1号  datetime.date(datetime.date.today().year,datetime.date.today().month,1)  #当月1号  datetime.date.today().replace(day=1)  #上月1号  (datetime.date.today().replace(day=1) - datetime.timedelta(1)).replace(day=1) | 
Python时间获取及转换知识汇总的更多相关文章
- 【转】time 模块详解(时间获取和转换)
		
转自鱼C论坛--https://fishc.com.cn/forum.php?mod=viewthread&tid=51326&highlight=time time 模块 -- 时间 ...
 - python 27 获取时区转换后的时间
		
python3的datetime有timezone属性,这里介绍python2.7环境下,获取时区转换后的时间. 利用第三方插件,pytz,没有安装的话安装一下. #!/usr/bin/env pyt ...
 - Python 时间获取
		
摘自:http://www.jb51.net/article/91365.htm 摘自:https://www.cnblogs.com/liuq/p/6211005.html 一.在python中,除 ...
 - Python时间获取详解,Django获取时间详解,模板中获取时间详解(navie时间和aware时间)
		
1.Python获取到的时间 import pytz from datetime import datetime now = datetime.now() # 这个时间为navie时间(自己不知道自己 ...
 - python时间模块详解(time模块)
		
time 模块 -- 时间获取和转换 time 模块提供各种时间相关的功能 在 Python 中,与时间处理有关的模块包括:time,datetime 以及 calendar 必要说明: 虽然这个模块 ...
 - python 时间与时间戳之间的转换
		
https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...
 - python  时间和时间戳的转换
		
对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...
 - js时间和时间戳之间如何转换(汇总)
		
js时间和时间戳之间如何转换(汇总) 一.总结 一句话总结: 1.js中通过new Date()来获取时间对象, 2.这个时间对象可以通过getTime()方法获取时间戳, 3.也可以通过getYea ...
 - python—时间与时间戳之间的转换
		
python-时间与时间戳之间的转换 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块 ...
 
随机推荐
- js对URL的相关操作集锦
			
1.location.href..... (1)self.loction.href="/url" window.location.href="/url" ...
 - Sass学习日志
			
一.什么是SASS SASS是一中CSS的开发工具,提供了许多便利的写法,大大节约了设计者们的时间,使得CSS的开发,变得简单和可维护.本文总结了SASS的主要方法.我们的目标是,有了这篇文章,日常的 ...
 - 手动创建简单webpack项目及React使用
			
一.创建基本的webpack4.x项目 1.运行 npm init -y 快速初始化项目 2.在项目根目录创建src的源代码目录和dist产品目录 3.在src目录下创建 index.html 4.使 ...
 - VS2013入门驱动配置测试
			
准备工作: VS2013 WDK8.1 DbgView InstDrv VS2013+WDK8.1是绝配,意思是这两个版本结合最方便,安装后无需任何改动直接写代码,自动生成模板,省去了设置一些参数繁琐 ...
 - linux 2.6升级Python2.7 ./configure 报错问题
			
升级2.7.3使用命令./configure --prefix=/usr/local/python2.7.3时,出现以下错误:checking build system type... x86_64- ...
 - Linux系统运维基础测试题
			
1 Linux运维基础测试题(第一关) 通过这段时间学习Linux基础命令,为了检测自己对Linux基础命令掌握的情况,从网上整理13到测试题,并将其整理出来供大家参考学习. 1.1 习题 ...
 - Angular2入门学习
			
最近项目使用angular2,1和2版本变化大变样.下面总结一些学习网址及安装步骤. 中文官网(必看): https://angular.cn 懒人学习: http://www.imooc.com/l ...
 - nginx2goaccess.sh脚本内容
			
脚本github地址:https://github.com/stockrt/nginx2goaccess/blob/master/nginx2goaccess.sh 脚本内容: #!/bin/bash ...
 - zookeeper相关知识与集群搭建
			
Zookeeper Zookeeper相关概念 Zookeeper概述 Zookeeper是一个分布式协调服务的开源框架,主要用来解决分布式集群中应用系统的一致性问题. Zookeeper本质上是一个 ...
 - 微信小程序播放视频
			
<view class="section tc"> <video id="myVideo" src="http://wxsnsdy. ...