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日期的更多相关文章

  1. Python日期时间函数处理

    所有日期.时间的 api 都在datetime模块内. 1 日期的格式化输出 datetime => string import datetime now = datetime.datetime ...

  2. Python 日期和时间(转)

    Python 日期和时间 Python程序能用很多方式处理日期和时间.转换日期格式是一个常见的例行琐事.Python有一个 time 和 calendar 模组可以帮忙. 什么是Tick? 时间间隔是 ...

  3. (转)Python 日期和时间

    转自http://www.runoob.com/python/python-date-time.html Python 日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见 ...

  4. 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码

    本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...

  5. Python 日期时间处理模块学习笔记

    来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...

  6. Python 日期和时间 —— datetime

    Python 日期和时间 —— datetime Python提供了多个内置模块用于操作日期时间,如calendar,time,datetime.calendar用于处理日历相关 :time提供的接口 ...

  7. 【310】◀▶ Python 日期和时间

    参考: python 时间日期计算 Python 日期和时间(菜鸟教程) 8.1. datetime — Basic date and time types python中datetime模块中dat ...

  8. Python 日期和时间戳的转换

    Python 日期和时间戳的转换 1. Python中处理时间的模块 Python中处理时间的模块有time.datetime和calendar. 在Python中表示时间的方式: 时间戳:10位整数 ...

  9. 练习十六:Python日期格式应用(datetime)

    练习:关于python日期格式应用练习.用python方法如何输出指定格式形式的日期 这里用到datetime模块,datetime模块重新封装了time模块,提供了更多接口,提供的类包括:date, ...

  10. python 日期、时间、字符串相互转换

    python 日期.时间.字符串相互转换 在python中,日期类型date和日期时间类型dateTime是不能比较的. (1)如果要比较,可以将dateTime转换为date,date不能直接转换为 ...

随机推荐

  1. Radar Installation(利用数据有序化进行贪心选择)

    English appre: an infinite straight line:一条无限长的直线 on the coasting:在海岸线上 Cartesian coordinate system, ...

  2. 使用 Razor 表达式

    https://blog.csdn.net/github_37410569/article/details/54986136 https://blog.csdn.net/qq_21419015/art ...

  3. C++-POJ1021-2D-Nim[hash]

    哈希,对于每个点哈希一次 哈希的方式:该点到联通分量边界(上下左右)的距离和 然后分别对两个图的n个点按hash值排序,判断是否相等即可 #include <set> #include & ...

  4. Apache Kafka(七)- Kafka ElasticSearch Comsumer

    Kafka ElasticSearch Consumer 对于Kafka Consumer,我们会写一个例子用于消费Kafka 数据传输到ElasticSearch. 1. 构造ElasticSear ...

  5. Go_json

    package main import ( "encoding/json" "fmt" ) // 结构体与json // 1.序列化: 把Go语言中的结构体变量 ...

  6. Java实现JSONObject对象与Json字符串互相转换

    Java实现JSONObject对象与Json字符串互相转换 JSONObject 转 JSON 字符串 Java代码: JSONObject jsonObject = new JSONObject( ...

  7. python numpy中sum()时出现负值

    import numpy a=numpy.random.randint(1, 4095, (5000,5000)) a.sum() 结果为负值, 这是错误的,a.sum()的类型为 int32,如何做 ...

  8. Elasticsearch系列---shard内部原理

    概要 本篇我们来看看shard内部的一些操作原理,了解一下人家是怎么玩的. 倒排索引 倒排索引的结构,是非常适合用来做搜索的,Elasticsearch会为索引的每个index为analyzed的字段 ...

  9. 同步块:synchronized(同步监视器对象){同步运行代码片段}

    package seday10; import seday03.Test2; /** * @author xingsir * 同步块:synchronized(同步监视器对象){需要同步运行的代码片段 ...

  10. Django框架-模板层

    Django框架-模板层 一.模板语法传值 1.验证是否python所有的数据类型都可以传递到前端 locals()的妙用:该方法虽然好用,但是在某些情况下会造成资源的浪费 结论:整型.浮点型.字符串 ...