Python日期
1. datatime
from datetime import datetime, date now = datetime.now()
print(now) # 2020-01-20 01:24:01.843183 to = date.today()
print(to) # 2020-01-20 print(datetime(2000, 1, 1, 1, 0, 0)) # 2000-01-01 01:00:00 delta = now - datetime(2000, 1, 1, 1, 0, 0)
print(now, type(now)) # 2020-01-20 01:24:01.843183 <class 'datetime.datetime'> print(now.date()) # 2020-01-20 print(now.time()) # 2020-01-20 print(delta) # 7324 days, 0:24:01.843183 print(delta.days) # 7324
1.1 strptime
from datetime import datetime
S=datetime.strptime('2019/07/07','%Y/%m/%d')
print(S,type(S))
S=datetime.strptime('2019年7月7日星期日','%Y年%m月%d日星期日')
print(S,type(S))
S=datetime.strptime('2019年7月7日星期日8时42分24秒','%Y年%m月%d日星期日%H时%M分%S秒')
print(S,type(S))
S=datetime.strptime('7/7/2019','%m/%d/%Y')
print(S,type(S))
S=datetime.strptime('7/7/2019 8:42:50','%m/%d/%Y %H:%M:%S')
print(S,type(S)) #结果:
2019-07-07 00:00:00 <class 'datetime.datetime'>
2019-07-07 00:00:00 <class 'datetime.datetime'>
2019-07-07 08:42:24 <class 'datetime.datetime'>
2019-07-07 00:00:00 <class 'datetime.datetime'>
2019-07-07 08:42:50 <class 'datetime.datetime'>
1.2 strftime
dt=datetime.now()
s=dt.strftime('%m/%d/%Y %H:%M:%S %p')
print(s,type(s))
s=dt.strftime('%A,%B %d,%Y')
print(s,type(s))
txt =('%s年%s月%s日星期%s %s时%s分%s秒'%(dt.strftime('%Y'),dt.strftime('%m'),dt.strftime('%d'),\<br>dt.strftime('%w'),dt.strftime('%H'),dt.strftime('%M'),dt.strftime('%S')))
print(txt)
s=dt.strftime('%B %d,%Y')
print(s,type(s)) 结果:
07/07/2019 14:57:17 PM <class 'str'>
Sunday,July 07,2019 <class 'str'>
2019年07月07日星期0 14时57分17秒
July 07,2019 <class 'str'>
2. time
import time # (1)当前时间戳
# time.time()
one = time.time()
print(one) # 1579454251.8242934 # (2)时间戳 → 元组时间
two = time.localtime(time.time())
print(two) # time.struct_time(tm_year=2020, tm_mon=1, tm_mday=20, tm_hour=1, tm_min=17, tm_sec=31, tm_wday=0, tm_yday=20, tm_isdst=0) # (3)时间戳 → 可视化时间
three = time.ctime(time.time())
print(three) # Mon Jan 20 01:17:31 2020 # (4)元组时间 → 时间戳
four = time.mktime(two)
print(four) # 1579454251.0 # (5)元组时间 → 可视化时间
five = time.asctime()
print(five) # Mon Jan 20 01:17:31 2020 # (6)时间元组 → 可视化时间(定制)
six = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(six) # 2020-01-20 01:17:31 # (7)可视化时间(定制) → 时间元祖
print("时间元祖:", time.strptime('2019-7-7 11:32:23', '%Y-%m-%d %H:%M:%S'))
# 时间元祖: time.struct_time(tm_year=2019, tm_mon=7, tm_mday=7, tm_hour=11, tm_min=32, tm_sec=23, tm_wday=6, tm_yday=188, tm_isdst=-1) # (8)将格式字符串转换为时间戳
eight = "Sun Jul 7 10:48:24 2019"
print(time.mktime(time.strptime(eight, "%a %b %d %H:%M:%S %Y")))
# 1562467704.0
Python日期的更多相关文章
- Python日期时间函数处理
所有日期.时间的 api 都在datetime模块内. 1 日期的格式化输出 datetime => string import datetime now = datetime.datetime ...
- Python 日期和时间(转)
Python 日期和时间 Python程序能用很多方式处理日期和时间.转换日期格式是一个常见的例行琐事.Python有一个 time 和 calendar 模组可以帮忙. 什么是Tick? 时间间隔是 ...
- (转)Python 日期和时间
转自http://www.runoob.com/python/python-date-time.html Python 日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见 ...
- 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...
- Python 日期时间处理模块学习笔记
来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...
- Python 日期和时间 —— datetime
Python 日期和时间 —— datetime Python提供了多个内置模块用于操作日期时间,如calendar,time,datetime.calendar用于处理日历相关 :time提供的接口 ...
- 【310】◀▶ Python 日期和时间
参考: python 时间日期计算 Python 日期和时间(菜鸟教程) 8.1. datetime — Basic date and time types python中datetime模块中dat ...
- Python 日期和时间戳的转换
Python 日期和时间戳的转换 1. Python中处理时间的模块 Python中处理时间的模块有time.datetime和calendar. 在Python中表示时间的方式: 时间戳:10位整数 ...
- 练习十六:Python日期格式应用(datetime)
练习:关于python日期格式应用练习.用python方法如何输出指定格式形式的日期 这里用到datetime模块,datetime模块重新封装了time模块,提供了更多接口,提供的类包括:date, ...
- python 日期、时间、字符串相互转换
python 日期.时间.字符串相互转换 在python中,日期类型date和日期时间类型dateTime是不能比较的. (1)如果要比较,可以将dateTime转换为date,date不能直接转换为 ...
随机推荐
- docker usage (2)
1. docker command docker start postgres docker container ls --all docker image ls --all docker ps -a ...
- java基础之I/O操作
字节流 直接上代码: import java.io.*; class Test{ public static void main(String[] args){ FileInputStream inp ...
- Oracle 11g安装 —— Oracle Database 11g Release2 for Windows(x64)
文章来自:https://blog.csdn.net/IT_xiao_guang_guang/article/details/104422421 下面是我的Oracle 11g安装过程,希望可以帮到正 ...
- IDEA与Tomcat相关配置
idea会为每一个Tomcat部署的项目,独立建一份配置文件. 配置文件所在位置 怎么部署的(查看虚拟目录)使用的第三种部署方式 部署项目存放的路径 项目目录和Tomcat部署目录 Tomcat真正访 ...
- StringBuilder与String的区别
String 在进行运算时(如赋值.拼接等)会产生一个新的实例,而 StringBuilder 则不会.所以在大量字符串拼接或频繁对某一字符串进行操作时最好使用 StringBuilder,不要使用 ...
- index unique scan 与index range scan等的区别
存取Oracle当中扫描数据的方法(一) Oracle 是一个面向Internet计算环境的数据库.它是在数据库领域一直处于领先地位的甲骨文公司的产品.可以说Oracle关系数据库系统是目前世界上流行 ...
- C#堆和栈的入门理解
声明:以下内容从网络整理,非原创,适当待入个人理解. 解释1.栈是编译期间就分配好的内存空间,因此你的代码中必须就栈的大小有明确的定义:堆是程序运行期间动态分配的内存空间,你可以根据程序的运行情况确定 ...
- opencv:霍夫直线检测
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- [消灭虫子] Qt 套接字发不出去东西
最近状态不是太好,导致出现bug之后心烦意乱的 遇到这样的事情,就是在 这样的函数执行之后服务端收不到任何消息,但是在调试台显示已经发送了正常的字节数. 表示大概已经发出去了,但是我确定服务端没有问题 ...
- Java - Test - TestNG: Idea 添加 TestNG 依赖
1. 概述 Idea 开发的 maven 添加 testng 依赖 2. 背景 最近复习 TestNG 尝试搭建环境 发现教材和网上很多的教程, 都是 eclipse 的 我用的是 idea 这个貌似 ...