'''
标准库:
1.time
时间的三种表示方法:a:时间戳(timestamp) b:格式化的时间字符串 c:元组(struct_time)共九个元素
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=7, tm_hour=22, tm_min=28, tm_sec=9, tm_wday=2, tm_yday=38, tm_isdst=0不是夏令时)
'''
import time
print(time.localtime())#元组(struct_time)
print(time.time())#获取时间戳(timestamp) help(time)#查看time模块使用方法 注:weekday (0-6, Monday is 0) print(time.timezone)#世界标准时间UTC与本地标准时间的差值,单位:s
print(time.timezone/3600)#结果是-8,因为中国在东八区
print(time.altzone)#世界标准时间UTC与夏令时时间的差值,单位:s
print(time.daylight)#是否使用了夏令时
time.sleep(2)#程序睡两秒 '''
将时间戳(timestamp)转表示换成元组(struct_time)表示形式:
gmtime:结果为UTC时区
localtime:结果为本地时区(中国:UTC+8时区)
'''
#time.gmtime()将时间戳(timestamp)转表示换成元组(struct_time)表示,括号内写时间戳(timestamp),若不写,则传入当前时间,将时间转换成UTC时间
print(time.gmtime())
print(time.gmtime(3))
#time.localtime将时间戳(timestamp)转表示换成元组(struct_time)表示,括号内写时间戳(timestamp),若不写,则传入当前时间,将时间转换成本地时间
print(time.localtime())
print(time.localtime(3))
x=time.localtime()
print(x.tm_year,x.tm_mon) '''
将元组(struct_time)表示转换成时间戳(timestamp)表示形式:mktime
'''
print(time.mktime(x)) '''
将元组(struct_time)表示转换成格式化的字符串表示形式:
A:strftime('格式',struct_time)---->‘格式化的字符串’
B:asctime 将struct_time转换成%a %b %d %H:%M:%S %Y
'''
print(help(time.strftime))
print(time.strftime('%d-%m-%Y,%H:%M:%S',x))# %Y:x.tm_year %m:x.tm_mon
print(time.strftime('%Y-%m-%d,%H:%M:%S',time.localtime())) print(help(time.asctime))
# Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
# When the time tuple is not present, current time as returned by localtime() is used.
print(time.asctime()) '''
将格式化的字符串表示转换成元组(struct_time)表示形式:
strptime('格式化的字符串',格式)---->‘struct_time’
'''
print(help(time.strptime))
print(time.strptime('2018-02-07,23:11:34','%Y-%m-%d,%H:%M:%S'))#x.tm_year=2018 x.tm_mon=02 .... '''
将时间戳(timestamp)转化成字符串表示形式:
ctime 将时间戳(timestamp)转换成%a %b %d %H:%M:%S %Y
'''
print(help(time.ctime))
# Convert a time in seconds since the Epoch to a string in local time.
# This is equivalent to asctime(localtime(seconds)).
# When the time tuple is not present, current time as returned by localtime() is used.
print(time.ctime()) # 2.datetime
import datetime
print(datetime.datetime.now())
'注:datetime.timedelta()必须和datetime.datetime.now()连用'
print(datetime.datetime.now()+datetime.timedelta(3))#当前时间+3天
print(datetime.datetime.now()+datetime.timedelta(-3))#当前时间-3天
print(datetime.datetime.now()+datetime.timedelta(hours=3))#当前时间+3小时
print(datetime.datetime.now()+datetime.timedelta(hours=-3))#当前时间-3小时
print(datetime.datetime.now()+datetime.timedelta(minutes=30))
'修改当前时间'
t=datetime.datetime.now()
print(t.replace(minute=3,hour=3))#时间替换

python_81_标准库_时间模块的更多相关文章

  1. python常用标准库(时间模块 time和datetime)

    常用的标准库 time时间模块 import time time -- 获取本地时间戳 时间戳又被称之为是Unix时间戳,原本是在Unix系统中的计时工具. 它的含义是从1970年1月1日(UTC/G ...

  2. Python标准库之时间模块time与datatime模块详解

    时间模块time与datatime 时间表示方式: 时间戳 格式化时间字符串 元组 时间戳格式: time.time()#输出1581664531.749063 元组格式: time.localtim ...

  3. (转)python标准库中socket模块详解

    python标准库中socket模块详解 socket模块简介 原文:http://www.lybbn.cn/data/datas.php?yw=71 网络上的两个程序通过一个双向的通信连接实现数据的 ...

  4. python——模块、标准库、第三方模块安装

    模块(module)简介 模块化--指将一个完整的程序分解为一个一个小的模块,通过将模块组合,来搭建出一个完整的程序. 模块化的特点: ① 方便开发 ② 方便维护 ③ 模块可以复用! 在Python中 ...

  5. Python学习笔记011_模块_标准库_第三方库的安装

    容器 -> 数据的封装 函数 -> 语句的封装 类 -> 方法和属性的封装 模块 -> 模块就是程序 , 保存每个.py文件 # 创建了一个hello.py的文件,它的内容如下 ...

  6. [C/C++标准库]_[初级]_[转换UTC时间到local本地时间]

    场景 1.如果有面向全球用户的网站, 一般在存储时间数据时存储的是UTC格式的时间, 这样时间是统一的, 并可以根据当地时区来进行准确的转换. 2.存储本地时间的问题就在于如果换了时区, 那么显示的时 ...

  7. Python标准库之logging模块

    很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...

  8. python常用标准库(压缩包模块zipfile和tarfile)

    常用的标准库 在我们常用的系统windows和Linux系统中有很多支持的压缩包格式,包括但不限于以下种类:rar.zip.tar,以下的标准库的作用就是用于压缩解压缩其中一些格式的压缩包. zip格 ...

  9. [C/C++标准库]_[0基础]_[交集和补集]

    场景: 1. 计算std::vector A和 std::vector B里的同样的元素, 用于保留不删除. 2. 计算std::vector A和 std::vector B里各自的补集, 用于删除 ...

随机推荐

  1. C#——传值参数(3)

    上篇文章我与大家共同学习了 值参数——引用类型这次与大家共同学习 传值参数--引用类型,不创建新对象,只操作对象这是个思维导图:我们仍需记住:1.值参数创建变量的副本 2.对值参数的改变不会影响变量的 ...

  2. CentOS6.5安装sqlite3[转]

    1.下载安装包:https://www.sqlite.org/download.html 2.解压 [root@mycentos ~]# tar xzvf sqlite-snapshot-201809 ...

  3. js实现考试随机选题

    考试的时候经常用到,发在这里记录一下 基本信息包括: 学号.姓名.题号.题目名称 实现原理:给每一个题目添加一个编号,JS生成随机数,遍历每一个学生,把题目根据生成的随机数作为题目编号放入学生信息中 ...

  4. 解决Idea项目启动报错:程序包javax.servlet.http不存在

    报错信息 在没有使用maven的时候,web项目从远程仓库获取下以后,起一次启动往往会报错javax.servlet.http程序包找不到,随之而来的java基础包都将不能使用,报错信息如下: 解决方 ...

  5. II play with GG(思维规律)

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 IG won the S champion ...

  6. 2017 ACM/ICPC Asia Regional Shenyang Online cable cable cable

    Problem Description Connecting the display screen and signal sources which produce different color s ...

  7. net core 2.0 web api + Identity Server 4 + angular 5

    net core 2.0 web api + Identity Server 4 + angular 5前台使用angular 5, 后台是asp.net core 2.0 web api + ide ...

  8. Kestrel服务器

    Kestrel服务器 什么是Kestrel服务器 Kestrel是开源的(GitHub提供的源代码),事件驱动的异步I / O服务器,用于在任何平台上托管ASP.NET应用程序.这是一个监听服务器和一 ...

  9. 持续集成~Jenkins构建dotnetCore的项目

    上周一个大件就是dotnet core2.0发布了,伴随着.NET Standard2.0也发布了,整个微软的生态环境大好,当然也有一个BUG出来了,比如EFCore对Mysql的支持比起1.1来说, ...

  10. plsql过期注册

    Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769password:xs374ca 打开plsql工具 点击注册即可