原文地址:http://www.bugingcode.com/blog/python_datetime.html

datatime 模块题共用一些处理日期,时间和时间间隔的函数。这个模块使用面向对象的交互取代了time模块中整形/元组类型的时间函数。

在这个模块中的所有类型都是新型类,能够从python中继承和扩展。

这个模块包含如下的类型:

  • datetime代表了日期和一天的时间

  • date代表日期,在1到9999之间

  • time 代表时间和独立日期。

  • timedelta 代表两个时间或者日期的间隔

  • tzinfo 实现时区支持

datetime 类型代表了某一个时区的日期和时间,除非有特殊说明,datetime对象使用的是“naive time”,也就是说程序中datetime的时间跟所在的时区有关联。datetime模块提供了一些时区的支持。

datetime

给定一个时间创建datetime对象,可以使用datetime构造函数:

import datetime

now = datetime.datetime(2003, 8, 4, 12, 30, 45)

print now
print repr(now)
print type(now)
print now.year, now.month, now.day
print now.hour, now.minute, now.second
print now.microsecond

结果如下:

$ python datetime-example-1.py
2003-08-04 12:30:45
datetime.datetime(2003, 8, 4, 12, 30, 45)
<type 'datetime.datetime'>
2003 8 4
12 30 45
0

值得注意的是默认字符串代表的是ISO 8601-style时间戳。

你也可以使用内建工厂函数来创建datetime对象(所有的这样的函数提供的是类方法,而不是模块函数)。

import datetime
import time print datetime.datetime(2003, 8, 4, 21, 41, 43) print datetime.datetime.today()
print datetime.datetime.now()
print datetime.datetime.fromtimestamp(time.time()) print datetime.datetime.utcnow()
print datetime.datetime.utcfromtimestamp(time.time())

结果如下:

$ python datetime-example-2.py
2003-08-04 21:41:43
2003-08-04 21:41:43.522000
2003-08-04 21:41:43.522000
2003-08-04 21:41:43.522000
2003-08-04 19:41:43.532000
2003-08-04 19:41:43.532000

像在这些例子中,时间对象的默认格式是ISO 8601-style 字符串:“yyyy-mm-dd hh:mm:ss”,毫秒是可选的,可以有也可以没有。

datetime类型提供另一种格式方法,包括高度通用的strftime方法(在time模块中可以看到更加详细的说明)。

import datetime
import time now = datetime.datetime.now() print now
print now.ctime()
print now.isoformat()
print now.strftime("%Y%m%dT%H%M%S")
$ python datetime-example-3.py
2003-08-05 21:36:11.590000
Tue Aug 5 21:36:11 2003
2003-08-05T21:36:11.590000
20030805T213611

date和time

date代表着datetime对象中日期部分。

import datetime

d = datetime.date(2003, 7, 29)

print d
print d.year, d.month, d.day print datetime.date.today()

结果如下:

$ python datetime-example-4.py
2003-07-29
2003 7 29
2003-08-07

time也类似,他代表着时间的一部分,以毫秒级为单位。

import datetime

t = datetime.time(18, 54, 32)

print t
print t.hour, t.minute, t.second, t.microsecond

结果如下:

$ python datetime-example-5.py
18:54:32

datetime提供datetime对象的扩展方法,同时也提供了转换两个对象到一个时间对象上的类方法。

import datetime

now = datetime.datetime.now()

d = now.date()
t = now.time() print now
print d, t
print datetime.datetime.combine(d, t)

结果如下:

$ python datetime-example-6.py
2003-08-07 23:19:57.926000
2003-08-07 23:19:57.926000
2003-08-07 23:19:57.926000

转载请标明来之:阿猫学编程

更多教程:阿猫学编程-python基础教程

python标准库:datetime模块的更多相关文章

  1. [python标准库]Pickle模块

    Pickle-------python对象序列化 本文主要阐述以下几点: 1.pickle模块简介 2.pickle模块提供的方法 3.注意事项 4.实例解析 1.pickle模块简介 The pic ...

  2. Python 标准库 ConfigParser 模块 的使用

    Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ...

  3. Python标准库——collections模块的Counter类

    1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...

  4. [python标准库]XML模块

    1.什么是XML XML是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词. ...

  5. 【python】Python标准库defaultdict模块

    来源:http://www.ynpxrz.com/n1031711c2023.aspx Python标准库中collections对集合类型的数据结构进行了很多拓展操作,这些操作在我们使用集合的时候会 ...

  6. Python标准库--os模块

    这个模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Windows下运行.一个例 ...

  7. python标准库 bisect模块

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #bisect #作用:维护有序列表,而不必在每次向列表增加一个元素 ...

  8. python标准库 sysconfig模块

    # -*- coding: utf-8 -*-# python:2.x__author__ = 'Administrator'import sysconfig#sysconfig:解释器编译时配置#作 ...

  9. Python标准库--time模块的详解

    time模块 - - -时间获取和转换 在我们学习time模块之前需要对以下的概念进行了解: 时间戳:时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08 ...

  10. Python标准库 -- UUID模块(生成唯一标识)

    UUID是什么: UUID: 通用唯一标识符 ( Universally Unique Identifier ),对于所有的UUID它可以保证在空间和时间上的唯一性,也称为GUID,全称为: UUID ...

随机推荐

  1. 吴裕雄--天生自然 JAVA开发学习:Scanner 类

    import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanne ...

  2. PAT Advanced 1024 Palindromic Number (25) [数学问题-⼤整数相加]

    题目 A number that will be the same when it is written forwards or backwards is known as a Palindromic ...

  3. nginx中rewrite flag

    rewrite  正则表达式  新URI  [flag]; [flag] 选项用于调控重写的行为,它的取值可能是: last:重写完成后,会停止继续处理当前区块所有属于ngx_http_rewrite ...

  4. Linux环境系列 之【配置虚拟机】

    前言懒得讲, 不给人解决问题的程序员不是好的程序员,所有老规矩,直接上干货 This is 系列故事 如果你从这里单独看那是没有任何问题的,但是你是小白的话建议你跳去 ☞从头开始[环境概述] ☞本章节 ...

  5. ZJNU 1262 - 电灯泡——中高级

    在影子没有到达墙角前,人越远离电灯,影子越长,所以这一部分无需考虑 所以只需要考虑墙上影子和地上影子同时存在的情况 因为在某一状态存在着最值 所以如果以影子总长与人的位置绘制y-x图像 会呈一个类似y ...

  6. ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction hihocoder1870~1879

    ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction hihocoder1870~1879 A 签到,dfs 或者 floyd 都行. #i ...

  7. CodeForces 990D Graph And Its Complement(图和补图、构造)

    http://codeforces.com/problemset/problem/990/D 题意: 构造一张n阶简单无向图G,使得其连通分支个数为a,且其补图的连通分支个数为b. 题解: 第一眼看到 ...

  8. 七、Shell脚本高级编程实战第七部

    一.写网络服务的系统启动脚本 利用case语句开发类似系统启动rsync服务的脚本 代码: #!/bin/sah. /etc/init.d/functionspidfile="/var/ru ...

  9. urlopen error [errno 10060]的解决思路

    当用多线程爬取某个网站的数据的时候,爬取一段时间后,总出现urlopen error [errno 10060]的错误,结果线程无端的被挂掉,一开始的解决思路是每次连接的时候换用不同的useragen ...

  10. PAT甲级——1077.Kuchiguse(20分)

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...