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. python property(不动产)方法

    class Test(object): @property def test(self): return 100 @test.setter def test(self): return "修 ...

  2. 载 js验证密码 必须由大小写字母、数字和特殊字符组成

    转自:https://blog.csdn.net/weixin_43824935/article/details/93601064 密码长度8-16位 必须由大写字母,小写字母,数字,特殊符号组成 正 ...

  3. csrf跨站点请求伪造

    什么是csrf(跨站请求伪造) 伪造请求的定义有很多种,我将不是用户本意发出的请求统称为伪造请求(在用户不知情的情况下执行某些操作)xss的通过用户对浏览器的信任造成的,csrf是通过服务器对浏览器的 ...

  4. 801. 二进制中1的个数(lowbit(n)函数)

    给定一个长度为n的数列,请你求出数列中每个数的二进制表示中1的个数. 输入格式 第一行包含整数n. 第二行包含n个整数,表示整个数列. 输出格式 共一行,包含n个整数,其中的第 i 个数表示数列中的第 ...

  5. Custom LED Keychain, Small And Surefire Gifts

    The    LED Keychain    makes it easy for people to carry their keys with them and carry them with th ...

  6. 传奇HERO引擎给装备加套装属性技巧

    装备加套装在复古的版本里比较少,但在1.76极品,轻变传奇,微变传奇和迷失版本里面用得比较多,每个引擎的方法相差不多,但也有一些小区别,今天给大家讲解下HERO引擎加套装的技巧. 第一步:我们打开M2 ...

  7. flutter web 配置环境及运行(windows)

    此下 操作 都是基于 windows  一, 将镜像添加到 用户环境变量中 由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时镜像,大家可以将如下环境变量加入到用 ...

  8. mvc:annotation-driven的前缀 "mvc"未绑定

    缺少MVC的配置,正确配置如下: <beans xmlns="http://www.springframework.org/schema/beans"       xmlns ...

  9. PMP概略学习上--基本思想和概念

    1 前言 花了10天左右的时间,对PMP(Project Management Professional,项目管理专业人士)考试认证做了一个概略学习.此次学习的目的是整体了解项目管理知识,并不是以考试 ...

  10. 安全文件传输协议之SFTP的使用

    一.SFTP概述 在前几篇文章,我们讲到了文件传输协议FTP(File Transfer Protocol),那也是使用比较广泛的文件服务器,但是我们需要知道,Linux系统并不自带FTP程序 如果要 ...