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不能直接转换为 ...
随机推荐
- Centos7部署jenkins
1. 下载rpm包: a) 下载地址:https://pkg.jenkins.io/redhat-stable/ b) 点选一个下载即可,例如点选:“jen ...
- 题解 P4289 【[HAOI2008]移动玩具】
题目地址:https://www.luogu.com.cn/problem/P4289 题解原地址:https://createsj.blog.luogu.org/solution-p4289 让我们 ...
- bootstrap4的改进
- 如何强制文件上传git仓库里
1.首先在本地创建一个文件夹里写入一个txt文件 2.然后复制你仓库的地址 3.平时正常操作都是这样上传不上 4.没关系我们使用强制上传 代码是 git push -f “你的仓库地址” mast ...
- python selenium设计模式POM
POM模式是什么 页面对象模型(POM)是一种设计模式,用来管理维护一组web元素集的对象库 在POM模式下,应用程序的每一个页面都有一个对的page class 每一个page class维护着该w ...
- Virtual Judge POJ 2251 Dungeon Master
三维数组加宽搜 #include <stdlib.h> #include <string.h> #include <stdio.h> ; int c, k, h; ...
- 找到所有的txt文件并删除
1.find /oldboy/ -type f -name "*.txt" -delete 2.find /oldboy/ -type f -name "*.txt&qu ...
- 3.创建springboot程序
有两种方式创建springboot项目 第一种方式:在官网上创建(基本上不用) Spring官方提供了非常方便的工具 Spring Initializr:https://start.spring.io ...
- selenium chromedriver退出报错
记录使用python调用chromedriver时遇到的问题 代码: #!/usr/bin/env python #-*- coding:utf-8 -*- # author : fy # versi ...
- 《aelf经济和治理白皮书》重磅发布:为DAPP提供治理高效、价值驱动的生态环境
2020年2月17日,aelf正式发布<aelf经济和治理白皮书>,这是aelf继项目白皮书后,在aelf网络经济模型和治理模式方面的权威论述.<aelf经济和治理白皮书>描述 ...