6.9 time 模块

方法 含义 备注
time.time() 时间戳 1561013092.997079
time.strftime('%Y-%m-%d %H:%M:%S %p') 结构化时间struct_time 转 格式化的字符串 2019-06-20 10:21:13 AM
time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X') 格式化的字符串 转 结构化时间struct_time time.struct_time(tm_year=2011, tm_mon=5...)
time.localtime() 时间戳转结构化时间struct_time 东八区时间 time.struct_time(tm_year=2019,tm_mon...)
time.gmtime() 时间戳转结构化时间 UTC时区 time.struct_time(tm_year=2019,tm_mon=6...)
time.mktime(time.localtime() 将一个struct_time 转 为时间戳 15663646462642646
time.asctime(time.localtime() 将一个struct_time 转 为Linux显示风格 Thu Jun 20 14:32:05 2019
time.ctime(12312312321) 将一个时间戳 转 为Linux显示风格 Mon Feb 29 22:45:21 2360

1、时间戳(以秒计算)

import time
print(time.time())
start_time=time.time()
time.sleep(3)
stop_time=time.time()
print(stop_time-start_time)

2、格式化的字符串

print(time.strftime('%Y-%m-%d %H:%M:%S %p')) # 2019-06-20 10:21:13 AM
print(time.strftime('%Y-%m-%d %X %p')) # 2019-06-20 10:21:13 AM
strftime(format[, t]) #把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个元素越界,ValueError的错误将会被抛出。
print(time.strftime("%Y-%m-%d %X", time.localtime())) #2019-06-20 00:49:56

3、struct_time()对象

print(time.localtime()) # 上海:东八区 time.struct_time(tm_year=2019,tm_mon=6,tm_mday=20,tm_hour=10,                                       tm_min=24, tm_sec=52, tm_wday=3, tm_yday=171, tm_isdst=0)
print(time.localtime(1111111111)) #将秒转换成 time.struct_time()格式,不填默认当前时间
print(time.localtime().tm_year) #
print(time.localtime().tm_mday) #
print(time.gmtime()) #UTC时区 差八个小时
#time.struct_time(tm_year=2019,tm_mon=6,tm_mday=20,tm_hour=2,tm_min=29,tm_sec=51,tm_wday=3,tm_yday=171,tm_isdst=0)

4、 mktime( t ) : 将一个struct_time转化为时间戳

print(time.mktime(time.localtime()))    #

5、time.strptime()

print(time.strptime('2017/04/08','%Y/%m/%d'))
time.strptime(string[, format]) # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
print(time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X'))
#time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,
# tm_wday=3, tm_yday=125, tm_isdst=-1)
#在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。

6、time.asctime()

print(time.asctime(time.localtime()))# Thu Jun 20 14:32:05 2019
asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。
print(time.asctime())#如果没有参数,将会将time.localtime()作为参数传入。Sun Sep 11 00:43:43 2016

7、time.ctime()

print(time.ctime(12312312321)) # Mon Feb 29 22:45:21 2360
#ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
print(time.ctime()) # Sun Sep 11 00:46:38 2016
print(time.ctime(time.time())) # Sun Sep 11 00:46:38 2016

6.10 datetime 模块

方法 含义 备注
datetime.datetime.now()   2019-06-20 17:06:25.170859
datetime.datetime.now() + datetime.timedelta(days=3) 当前时间+3天 2019-06-23 17:14:24.660116
current_time.replace(year=1977) 更改当前时间 1977-06-20 17:18:11.543876
datetime.date.fromtimestamp(time.time()) 时间戳直接转成日期格式 2019-08-19
import datetime
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(minutes=30)) #当前时间+30分
current_time=datetime.datetime.now()
print(current_time.replace(year=1977))#1977-06-20 17:18:11.543876
print(datetime.date.fromtimestamp(1111111111))#2005-03-18
print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19

6.11 打印进度条

def progress(percent,width=50):
if percent > 1:
percent=1
show_str=('[%%-%ds]' %width) %(int(width*percent) * '#')
print('\r%s %d%%' %(show_str,int(100*percent)),end='')

import time
recv_size=0
total_size=8097
while recv_size < total_size:
time.sleep(0.1)
recv_size+=80
percent=recv_size / total_size
progress(percent)

python 之 time模块、datetime模块(打印进度条)的更多相关文章

  1. Python的time和datetime模块

    Python的time和datetime模块 time 常用的有time.time()和time.sleep()函数. import time print(time.time()) 149930555 ...

  2. python中time、datetime模块的使用

    目录 python中time.datetime模块的使用 1.前言 2.time模块 1.时间格式转换图 2.常用方法 3.datetime模块 python中time.datetime模块的使用 1 ...

  3. Python 入门之 内置模块 -- datetime模块

    Python 入门之 内置模块 -- datetime模块 1.datetime模块 from datetime import datetime (1)datetime.now() 获取当前时间和日期 ...

  4. Python模块01/自定义模块/time模块/datetime模块/random模块

    Python模块01/自定义模块/time模块/datetime模块/random模块 内容大纲 1.自定义模块 2.time模块 3.datetime模块 4.random模块 1.自定义模块 1. ...

  5. 来看看Python炫酷的颜色输出与进度条打印

    英语单词优化 上篇文章写到了Python开发英语单词记忆工具,其中依赖了bootstrap.css jQuery.js 基础html模块以及片段的css样式.有些朋友问,怎么能将这个练习题打包成单独的 ...

  6. 利用Python计算π的值,并显示进度条

    利用Python计算π的值,并显示进度条  第一步:下载tqdm 第二步;编写代码 from math import * from tqdm import tqdm from time import ...

  7. 打印进度条——(progress bar才是专业的)

    # 打印进度条——(progress bar是专业的) import time for i in range(0,101,2): time.sleep(0.1) char_num = i//2 #打印 ...

  8. python3如何打印进度条

    Python3 中打印进度条(#)信息: 代码: import sys,time for i in range(50): sys.stdout.write("#") sys.std ...

  9. Python中time和datetime模块的简单用法

    python中与时间相关的一个模块是time模块,datetime模块可以看为是time模块的高级封装. time模块中经常用到的有一下几个方法: time()用来获取时间戳,表示的结果为从1970年 ...

随机推荐

  1. Node.js学习笔记(1):Node.js快速开始

    Node.js学习笔记(1):Node.js快速开始 Node.js的安装 下载 官方网址:https://nodejs.org/en/ 说明: 在Windows上安装时务必选择全部组件,包括勾选Ad ...

  2. 基础 PHP 语法

    PHP 脚本在服务器上执行,然后向浏览器发送回纯 HTML 结果. 基础 PHP 语法 PHP 脚本可放置于文档中的任何位置. PHP 脚本以 <?php 开头,以 ?> 结尾: < ...

  3. Laravel的初始化安装

    Laravel的初始化安装 composer 安装 composer中国镜像laravel文档 curl -sS https://getcomposer.org/installer | php # 修 ...

  4. jQuery 3D旋转展示焦点图

    在线演示 本地下载

  5. 可信执行环境(TEE)介绍 与应用

    原文:http://blog.csdn.net/wed110/article/details/53894927 可信执行环境(TEE,Trusted Execution Environment) 是G ...

  6. IDEA 设置代码行宽度

    1.在File->settings->Editor->Code Style 2.在File->settings->Editor->Code Style->XM ...

  7. 在node.js中建立你的第一个HTTp服务器

    这一章节我们将从初学者的角度介绍如何建立一个简单的node.js HTTP 服务器 创建myFirstHTTPServer.js //Lets require/import the HTTP modu ...

  8. codeforces 703B B. Mishka and trip(数学)

    题目链接: B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input stan ...

  9. linux 下文件恢复工具extundelete介绍

        下载 http://extundelete.sourceforge.net/ bunzip2 extundelete-0.2.0.tar.bz2 tar xvf extundelete-0.2 ...

  10. 洛谷 P2285 [HNOI2004]打鼹鼠

    题目描述 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿牛编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时刻鼹鼠会在某一个网格探出头来透透气. ...