python的datetime模块处理时间
python的datetime模块主要用来处理时间,里面包含很多类,包括timedelay,date,time,datetime等
开发中经常会用到模块里面的datetime类,这是一个表示日期时间的类。
1.创建一个新的datetime对象
In []: from datetime import datetime In []: my_time = datetime(,,) In []: my_time
Out[]: datetime.datetime(, , , , )
datetime对象接受的参数形式 datetime(year, month, day, hour=0, minute=0, second=0) ,创建一个新的datetime对象时,必须传参数年月日
2.datetime对象的常用方法
datetime.today() 返回当前的时间
datetime.now(tz=None) 返回当前的时间
datatime.strptime(date_string, format) 将特定format形式的时间字符串转换为datetime对象
datetime.strftime(fortmat) 将datetime对象转换为format形式的字符串
比如:
In [13]: datetime.now() # 返回当前时间的datetime对象
Out[13]: datetime.datetime(2018, 8, 24, 13, 4, 19, 140295) In [14]: datetime.today() # 返回当前时间的datetime对象
Out[14]: datetime.datetime(2018, 8, 24, 13, 5, 8, 107883) # 将 '2018-2-16' 这种形式的字符串转换为datetime对象
In [15]: my_time = '2018-2-16' In [16]: datetime.strptime(my_time,'%Y-%m-%d')
Out[16]: datetime.datetime(2018, 2, 16, 0, 0) # 将 datetime.datetime(2018, 2, 16, 0, 0)对象转换为字符串 '2-16-2018'
In [17]: my_datetime = datetime.strptime(my_time, '%Y-%m-%d') In [18]: my_datetime.strftime('%m-%d-%Y')
Out[18]: '02-16-2018'
有关format的说明,format使用%和字母组成,表示特定的意义
Y:表示年,如 2018
m:表示月份 ,如 02
d:表示天数,如 06
H:表示小时,如 11
M:表示分钟,如 34
S:表示秒数,如 45
3.datetime对象的属性
datetime.year
datetime.month
datetime.day
datetime.hour
datetime.minute
In [19]: now_datetime = datetime.now() In [20]: now_datetime
Out[20]: datetime.datetime(2018, 8, 24, 13, 21, 48, 178007) In [21]: now_datetime.year
Out[21]: 2018 In [22]: now_datetime.month
Out[22]: 8 In [23]: now_datetime.day
Out[23]: 24 In [24]: now_datetime.hour
Out[24]: 13 In [25]: now_datetime.minute
Out[25]: 21 In [26]: now_datetime.second
Out[26]: 48
python的datetime模块处理时间的更多相关文章
- 基于Python的datetime模块和time模块源码阅读分析
目录 1 前言 2 datetime.pyi源码分步解析 2.1 头部定义源码分析 2.2 tzinfo类源码分析 2.3 date类源码分析 2.4 time类源码分析 2.5 timedelta ...
- 孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块
孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.datetime模块 dateti ...
- python使用datetime模块计算各种时间间隔的方法
python使用datetime模块计算各种时间间隔的方法 本文实例讲述了python使用datetime模块计算各种时间间隔的方法.分享给大家供大家参考.具体分析如下: python中通过datet ...
- python中datetime模块
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...
- Python,datetime模块实例
Python的标准模块datetime模块,在我们的工作中应用非常频繁,下面对datetime中常用的方法进行了总结和测试:对每一个方法都使用了单元测试框架Unittest来配合测试. 主要的类型有: ...
- Python的datetime模块分析
datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime ...
- python——从datetime模块探索python的数据架构
问题出现于我试图向自建网页中加入实时时间开始. 我之前已经知道python中有有关事件和日期的模块datetime.以下导入datetime并作实验. >>> import date ...
- python中 datetime模块的详解(转载)
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...
- Python time & datetime模块
time 模块 时间分为三种格式: 时间戳:表示1970年1月1日之后的秒 结构化时间:元组包含了:年.日.星期等... 格式化字符串:格式可以自定义 时间戳: import time time_st ...
随机推荐
- two's complement,2的补码
本文为作者原创,允许转载,但必须注明原文地址:https://www.cnblogs.com/byronxie/p/10117265.html Let's start with one questio ...
- Windows平台编译MySQL5.7源码
https://blog.csdn.net/linjingke32/article/details/85111711
- linux命令-awk入门
最近经常查看nginx日志,有时候需要做一些统计分析,于是就想起了awk,学习了就顺便做一个记录. 目录 概述:简单介绍awk背景原理 基本用法:常用到的awk语法 内建变量 综合实例 概述 awk是 ...
- MFC原理第六讲.消息传递
---恢复内容开始--- MFC原理第六讲.消息传递 一丶简介 通过上一讲我们的消息映射表.我们得知. 消息映射表 会保存父类的MessageMap 以及自己当前的消息结构体数组. 消息传递是一层一层 ...
- 【ASP.NET MVC系列】浅谈jqGrid 在ASP.NET MVC中增删改查
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
- MyBatis源码解析(七)——DataSource数据源模块之托管数据源
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6675700.html 1 回顾 之前介绍的非池型与池型数据源都是MyBatis自己定义的内 ...
- yum安装php Requires: libzip5(x86-64) >= 1.3.2
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- SpringBoot系列——aop 面向切面
前言 项目中我们经常会用到aop切面,比如日志记录:这里简单记录一下springboot是如何使用aop spring对aop的配置,来自springboot参考手册,Common applicati ...
- 南大算法设计与分析课程复习笔记(1) L1 - Model of computation
一.计算模型 1.1 定义: 我们在思考和处理算法的时候是机器无关.实现语言无关的.所有的算法运行在一种“抽象的机器”之上,这就是计算模型. 1.2 种类 图灵机是最有名的计算模型,本课使用更简单更合 ...
- C++程序实例唯一方案,窗口只打开一次,程序只打开一次
首先是方法: // IsAlreadyRunning - 是否已经运行 BOOL IsAlreadyRunning() { BOOL bRet = FALSE; HANDLE hMutex = ::C ...