Python 日期和时间

Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能。

Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。

时间间隔是以秒为单位的浮点小数。

每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示。

Python 的 time 模块下有很多函数可以转换常见日期格式。如函数time.time()用于获取当前时间戳, 如下实例:

#!/usr/bin/python
# -*- coding: UTF-8 -*- import time; # 引入time模块 ticks = time.time()
print "当前时间戳为:", ticks

  以上实例输出结果:

当前时间戳为: 1459994552.51

  

获取当前时间

从返回浮点数的时间辍方式向时间元组转换,只要将浮点数传递给如localtime之类的函数。

#!/usr/bin/python
# -*- coding: UTF-8 -*- import time localtime = time.localtime(time.time())
print "本地时间为 :", localtime

  以上实例输出结果:

本地时间为 : time.struct_time(tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=3, tm_sec=27, tm_wday=3, tm_yday=98, tm_isdst=0)

  

获取格式化的时间

你可以根据需求选取各种格式,但是最简单的获取可读的时间模式的函数是asctime():

#!/usr/bin/python
# -*- coding: UTF-8 -*- import time localtime = time.asctime( time.localtime(time.time()) )
print "本地时间为 :", localtime

  以上实例输出结果:

本地时间为 : Thu Apr  7 10:05:21 2016

  

格式化日期

我们可以使用 time 模块的 strftime 方法来格式化日期,:

time.strftime(format[, t])

  

#!/usr/bin/python
# -*- coding: UTF-8 -*- import time # 格式化成2016-03-20 11:45:39形式
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 格式化成Sat Mar 28 22:24:24 2016形式
print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) # 将格式字符串转换为时间戳
a = "Sat Mar 28 22:24:24 2016"
print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

  以上实例输出结果:

2016-04-07 10:25:09
Thu Apr 07 10:25:09 2016
1459175064.0

  

python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身

  

获取某月日历

Calendar模块有很广泛的方法用来处理年历和月历,例如打印某月的月历:

#!/usr/bin/python
# -*- coding: UTF-8 -*- import calendar cal = calendar.month(2016, 1)
print "以下输出2016年1月份的日历:"
print cal;

  以上实例输出结果:

以下输出2016年1月份的日历:
January 2016
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

  

Python中的时间模块和日期模块的更多相关文章

  1. Python中的时间与日期

    本文简要介绍datetime,time模块的简要用法. datetime模块 datetime模块主要有四个主要的对象. date 处理年.月.日 time处理时.分.秒.微秒 datetime处理日 ...

  2. python中的时间和时间格式转换

    1.python中的时间:要得到年月日时分秒的时间: import time #time.struct_time(tm_year=2012, tm_mon=9, tm_mday=15, tm_hour ...

  3. Python 中的时间处理包datetime和arrow

    Python 中的时间处理包datetime和arrow 在获取贝壳分的时候用到了时间处理函数,想要获取上个月时间包括年.月.日等 # 方法一: today = datetime.date.today ...

  4. Python中的时间日期模块(time、datetime)

    目录 Datetime 获取当前时间 获取当前日期 获取当前时间的tuple元组 格式化日期和时间 时间移动 获取两个时间的时间差 时间格式转换 Time 获取距元年(1970.1.1)的秒数 当时时 ...

  5. Python中的日志记录方案-logging模块&loguru模块

    原文链接 原创: 崔庆才 在 Python 中,一般情况下我们可能直接用自带的 logging 模块来记录日志,包括我之前的时候也是一样.在使用时我们需要配置一些 Handler.Formatter ...

  6. 解决 python 中,时间日期不能序列化的问题

    在python 中, 你在数据库娶到了数据中如果含有时间日期,那么你在向前端作为json对象传递的时候呢,就会报错.大致如下: TypeError: datetime.datetime(2017, 1 ...

  7. python中给程序加锁之fcntl模块的使用

    python 中给文件加锁——fcntl模块import fcntl 打开一个文件##当前目录下test文件要先存在,如果不存在会报错.或者以写的方式打开f = open('./test')对该文件加 ...

  8. Python中的上下文管理器(contextlib模块)

    上下文管理器的任务是:代码块执行前准备,代码块执行后收拾 1 如何使用上下文管理器: 打开一个文件,并写入"hello world" filename="my.txt&q ...

  9. python中的时间处理函数

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

随机推荐

  1. yum安装Development Tools报错问题

    yum安装Development Tools报错问题 我们通过yum安装Development Tools开发组工具的时候,有时可能会遇到如下报错信息. [root@superdesktop ~]# ...

  2. php面向对象重的抽象类,接口类与静态

    static 静态 <?php class ren { public $name; public static $sex; static function shao() { echo " ...

  3. 16/7/11_PHP-文件系统

    读取文件内容 PHP具有丰富的文件操作函数,最简单的读取文件的函数为file_get_contents,可以将整个文件全部读取到一个字符串中. $content = file_get_contents ...

  4. canvas绘制加载特效

    css样式: body{ text-align: center; } canvas{ background: #ddd; } canvas标签: <canvas id="canvas& ...

  5. poj1163The Triangle(动态规划,记忆化搜索)

    7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calc ...

  6. Shell生成随机密码

    #tr参数 -c或——complerment:取代所有不属于第一字符集的字符: -d或——delete:删除所有属于第一字符集的字符: 1.生成10个小写字母 [root@kafka60 shell] ...

  7. 爬虫(十一)—— XPath总结

    目录 XPath总结 一.何为XPath 二.XPath语法 1.语法 2.实例 三.XPath轴 1.XPath轴语法 2.XPath轴实例 四.XPath运算符 XPath总结 一.何为XPath ...

  8. spring-第二篇ApplicationContext国际化及事件机制

    1.ApplicationContext接口获取spring容器      ApplicationContext是BeanFactory接口的子接口,BeanFactory的常用实现类是Default ...

  9. [BZOJ4151]The Cave

    Solution: ​ 假设现在在点1,有许多形如 (x, y, z) 的限制条件,那么对于一组限制,必须先走到 x, y 的 \(\frac{z-dis(x, y)}{2}\) 级祖先,叫这些点为限 ...

  10. [已解决]报错: warning: LF will be replaced by CRLF in lib/anime.min.js

    git config --global core.autocrlf false