'''
标准库:
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. 深度卷积网络-Inception系列

    目录 1. Inception V1 1.1 Inception module 2. Inception V2 3. Inception V3 4. Inception V4, Inception-R ...

  2. 多行文字超出字数部分省略(主要解决不兼容;display: -webkit-box;的浏览器)

    注明:内容来处https://www.cnblogs.com/ss977/p/5846176.html 1.现webkit内核的浏览器支持display: -webkit-box;属性, 所以网页中显 ...

  3. [UE4]Component相关常用API

    http://www.dawnarc.com/2017/02/ue4component%E7%9B%B8%E5%85%B3%E5%B8%B8%E7%94%A8api/ Actor.h //获取第一个与 ...

  4. uoj#339. 【清华集训2017】小 Y 和二叉树(构造)

    传送门 膜拜大米饼巨巨 构造思路太神仙了-- 先考虑这个序列的开头,肯定是一个度数小于等于\(2\)且标号最小的节点,设为\(u\) 如果一个点度数小于等于\(2\),我们称这个点可以被选择,一个点的 ...

  5. VBA for AutoCAD

    Download the Microsoft Visual Basic for Applications Module (VBA) 2016 Downloads AutoCAD 2016 VBA mo ...

  6. selenium+Python搭建

    安装环境:windows 7 64位   1.安装python,版本为python2.7 1)下载安装包. 在python官方网站选择下载python2版本的windows安装包:python-2.7 ...

  7. SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Cache缓存简介 从Spring3开始定义Cache和Cac ...

  8. TensorFlow高层封装:从入门到喷这本书

    目录 TensorFlow高层封装:从入门到喷这本书 0. 写在前面 1. TensorFlow高层封装总览 2. Keras介绍 2.1 Keras基本用法 2.2 Keras高级用法 3. Est ...

  9. MySQL server has gone away和Maximum execution time of 120 seconds exceeded

    今天在写采集时碰到两个问题1.MySQL server has gone away2.Maximum execution time of 120 seconds exceeded 采集程序写好运行大概 ...

  10. 如何使用localStorage?

    首先使用localStorage的时候,我们需要判断浏览器是否支持localStorage这个属性. if(!window.localStorage){ alert("浏览器支持locals ...