import time

print(time.clock())
print(time.process_time())
print(time.time()) #返回当前系统时间戳
print(time.ctime()) #返回当前系统时间
print(time.ctime(time.time()-86640)) #将时间戳转为字符串
print(time.gmtime(time.time()-86640)) #将时间戳转为struct_time格式
print(time.localtime(time.time()-86640)) #将时间戳转为struct_time格式,但返回本地赶时间
print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time转回时间戳
#time.sleep(1) #睡眠4秒
print(time.strftime("%Y-%m-%d %H:%M:%S"),time.gmtime())#将struct_time转成指定字符串格式
print(time.strptime("2016-01-28","%Y-%m-%d")) #字符串格式转成struct_time import datetime print(datetime.date.today()) #输出格式 2016-05-25
print(datetime.date.fromtimestamp(time.time() - 864400)) #将时间戳转成日期格式
current_time = datetime.datetime.now()
print(current_time) #输出2016-05-25 19:33:49.521486
print(current_time.timetuple()) #返回struct_time格式
print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换
str_to_date = datetime.datetime.strptime("2016-05-25 19:42:00","%Y-%m-%d %H:%M:%S") #将字符串转换成日期格式
new_date = datetime.datetime.now() + datetime.timedelta(days=10)
new_date = datetime.datetime.now() + datetime.timedelta(days=-10)
new_date = datetime.datetime.now() + datetime.timedelta(hours=-10)
new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) 最常用的几个时间函数:
time.time():返回当前的时间戳
time.localtime([secs]):默认将当前时间戳转换成当前时区的struct_time
time.sleep([secs]):计时器
time.strftime(format,[,t]):把一个struct_time转换成格式化的时间字符串 函数支持的格式化函数如下:

参数

函义

   

%a

本地简化星期名

%A

本地完整星期名

%b

本地简化月名

%B

本地完整月名称

%c

本地相应的日期和时间表示

%d

一个月中的第几天

%H

一天中的第几个小时(24小时制,00-23)

%I

(12小时制01-12)

%j

一年中的第几天(001-366)

%m

月份

%M

分钟数

%p

本地am或者pm的响应值

%S

%U

一年中的星期数

%w

一星期中的第几天(0-6,0是星期天)

%W

和%U基本相同,不同的是它是以星期一为一个星期的开始

%x

本地相应的日期

%X

本地相应的时间

%y

简化的年份(00-99)

%Y

完年份

%Z

时区的名字

%%

%字符

												

python之时间函数的更多相关文章

  1. Python日期时间函数处理

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

  2. python语法------时间函数

    1.导入函数库: import time 获取格式化的时间 你可以根据需求选取各种格式,但是最简单的获取可读的时间模式的函数是asctime(): #!/usr/bin/python # -*- co ...

  3. Python日期时间函数

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

  4. Python与SQLite日期时间函数的使法

    SQLite的时间函数跟Python的时间函数有些许差别,所以稍做记录,供自己以后查询. 网上有将SQLite官方WIKI内容翻译成中文的文章,大家有兴趣可以搜索一下,我这里单纯记录一下个人比较常用的 ...

  5. Python日期时间Date/Time

    Python程序可以处理多种方式的日期和时间.日期格式之间的转换是一种常见计算机的杂活. Python的时间和日历模块,能帮助处理日期和时间. Tick是什么? 为时间间隔,以秒为单位的浮点数.从“新 ...

  6. Python常用时间操作总结【取得当前时间、时间函数、应用等】转载

    Python常用时间操作总结[取得当前时间.时间函数.应用等] 转载  2017-05-11   作者:清风乐逍遥    我要评论 这篇文章主要介绍了Python常用时间操作,包括取得当前时间.时间函 ...

  7. Python 关于时间和日期函数使用 -- (转)

    python中关于时间和日期函数有time和datatime   1.获取当前时间的两种方法: import datetime,time now = time.strftime("%Y-%m ...

  8. Python time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组

    Python time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组 import time dt=time.strptime('2019-08-08 11:32:23', ...

  9. python中时间的基本使用

    格式化日期 我们可以使用 time 模块的 strftime 方法来格式化日期,: time.strftime(format[, t]) #!/usr/bin/python # -*- coding: ...

随机推荐

  1. 【LeetCode】172. Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

  2. XAMPP Apache 配置多端口和多域名方法

    我们在工作中经常遇到同时调试多个网站的情况,那么如何配置呢?就像平时访问网站一样,网站 a.com 与网站 b.com 截然不同.这都是常见现象,如果在局域网中要访问另外一台电脑上的多个网站,就需要使 ...

  3. HTTP消息头

    (一)初识HTTP消息头 但凡搞WEB开发的人都离不开HTTP(超文本传输协议),而要了解HTTP,除了HTML本身以外,还有一部分不可忽视的就是HTTP消息头.做过Socket编程的人都知道,当我们 ...

  4. windows系统mysql定时自动备份

    MySQL Administrator 工具是MySQL官方的数据库管理工具,包含在MySQL GUI Tools中,可在MySQL官方网站下载到,下载地址:http://dev.mysql.com/ ...

  5. 浅谈__declspec(dllexport)和__declspec(dllimport)

    __declspec(dllimport)和__declspec(dllexport)经常是成对的,在动态链接库中__declspec(dllexport)导出dll中的成员,__declspec(d ...

  6. linux 将debug信息重定向到LCD(屏幕)

    /*********************************************************************** * linux 将debug信息重定向到LCD(屏幕) ...

  7. js校验表单后提交表单的三种方法总结(转)

    第一种: 代码如下: <script type="text/javascript"> function check(form) { if(form.userId.val ...

  8. 我看的公开课系列--《TED:对无知的追求》 by stuart firestein

    http://open.sina.com.cn/course/id_1047/ What scientists do is not just collect data and collect fact ...

  9. python 函数性能分析

    1 使用profile分析函数性能示例1, 以profile为例: import profile def profileTest(): Total =1; for i in range(10): To ...

  10. Linux(Ubuntu 13)下安装Eclipse

    1.本机配置及软件情况: Ubuntu版本:Ubuntukylin-13.10-desktop-i386(32位) JDK:jdk1.7.0_15(jdk-7u15-i856.gz) eclipse: ...