datetime模块定义了下面这几个类:

datetime.date:表示日期的类。常用的属性有year, month, day.

datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond.

datetime.datetime:表示日期时间。

datetime.timedelta:表示时间间隔,即两个时间点之间的长度。

datetime.tzinfo:与时区有关的相关信息

datetime类

datetime是date与time的结合体,包括date与time的所有信息。

它的构造函数如下:datetime.datetime (year, month, day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo]]]]] ),各参数的含义与date、time的构造函数中的一样,要注意参数值的范围。

datetime类定义的类属性与方法:

datetime.min、datetime.max:datetime所能表示的最小值与最大值;

datetime.resolution:datetime最小单位;

datetime.today():返回一个表示当前本地时间的datetime对象;

datetime.now([tz]):返回一个表示当前本地时间的datetime对象,如果提供了参数tz,则获取tz参数所指时区的本地时间;

datetime.utcnow():返回一个当前utc时间的datetime对象;

datetime.fromtimestamp(timestamp[, tz]):根据时间戮创建一个datetime对象,参数tz指定时区信息;

datetime.utcfromtimestamp(timestamp):根据时间戮创建一个datetime对象;

datetime.combine(date, time):根据date和time,创建一个datetime对象;

datetime.strptime(date_string, format):将格式字符串转换为datetime对象;

使用例子:

from datetime import *
import time
>>> print 'datetime.max:', datetime.max
datetime.max: 9999-12-31 23:59:59.999999
>>> print 'datetime.min:', datetime.min
datetime.min: 0001-01-01 00:00:00
>>> print 'datetime.resolution:', datetime.resolution
datetime.resolution: 0:00:00.000001
>>> print 'today():', datetime.today()
today(): 2012-02-24 22:17:36.945862
>>> print 'now():', datetime.now()
now(): 2012-02-24 22:17:36.966896
>>> print 'utcnow():', datetime.utcnow()
utcnow(): 2012-02-24 14:17:36.976883

datetime类提供的实例方法与属性:

datetime.year、month、day、hour、minute、second、microsecond、tzinfo:datetime.date():获取date对象;

datetime.time():获取time对象;

datetime.replace ([ year[ , month[ , day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo]]]]]]]] ):

datetime.timetuple()

datetime.utctimetuple()

datetime.toordinal()

datetime.weekday()

datetime.isocalendar()

datetime.isoformat ([ sep] )

datetime.ctime():返回一个日期时间的C格式字符串,等效于time.ctime(time.mktime(dt.timetuple()));

datetime.strftime(format)

像date一样,也可以对两个datetime对象进行比较,或者相减返回一个时间间隔对象,或者日期时间加上一个间隔返回一个新的日期时间对象。

Python datetime模块的datetime类的更多相关文章

  1. (转)python time模块和datetime模块详解

    python time模块和datetime模块详解 原文:http://www.cnblogs.com/tkqasn/p/6001134.html 一.time模块 time模块中时间表现的格式主要 ...

  2. Python常用模块(time, datetime, random, os, sys, hashlib)

    time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) :         通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运 ...

  3. python中datetime模块中datetime对象的使用方法

    本文只讲述datetime模块中datetime对象的一些常用的方法,如果读者需要更多datetime模块的信息,请查阅此文档. datetime模块的对象有如下: timedelta date da ...

  4. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  5. python time模块和datetime模块

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  6. python 时间模块(time ,datetime,calendar)

    Python中提供了时间相关的内置模块,我们主要用的是:time模块.datetime模块和calendar模块 ⽇期格式化的标准: %y 两位数的年份表示(00-99) %Y 四位数的年份表示(00 ...

  7. Python常用模块time & datetime &random 模块

    时间模块前言 在Python中,与时间处理有关的模块就包括:time,datetime 一.在Python中,通常有这几种方式来表示时间: 时间戳 格式化的时间字符串 元组(struct_time)共 ...

  8. Python常用模块:datetime

    使用前提: >>> from datetime import datetime 常见用法: 1.获取当前日期和时间 >>> now = datetime.now() ...

  9. Day 16 : Python 时间模块[time,]datetime[]及第三方模块的下载与安装

    在进行python程序开发时,除了可以使用python内置的标准模块外,还右许多第三方模块使用,可以在python官网找到. 在使用第三方模块时,需要下载并安装此模块,然后就可以使用标准模块一样导入并 ...

随机推荐

  1. 《剑指Offer》面试题-重建二叉树

    题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7 ...

  2. iOS基础 - UIScrollView

    一.UIScrollView使用引导思路. 1.之前我们所学的显示图片是用UIImageView 2.将UIImageView添加到根视图中,不显示的原因:没有设置位置 3.当图片比屏幕大时,直接放在 ...

  3. Intellij Idea/Webstorm/Phpstorm

    Intellij Idea/Webstorm/Phpstorm 的高效快捷键   1. shift + F6可以理解为F2的豪华重量版,不但可以重命名文件名,而且可以命名函数名,函数名可以搜索引用的文 ...

  4. 【javascript】判断浏览设备是手机还是电脑

    判断页面浏览设备是手机还是电脑,代码如下: function browserRedirect() { var sUserAgent = navigator.userAgent.toLowerCase( ...

  5. poj3984迷宫问题

    一个5 × 5的二维数组,表示一个迷宫.其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. 很简单的一道题,迷宫问题,一般都选择两种优先搜索 ...

  6. 实践Scrum

    http://blog.vsharing.com/agiledo/ [原创]敏捷开发实践-我们这样实践Scrum-续(81页) 我们团队实践Scrum,有一些做法和教训,希望和大家分享: 公司背景:通 ...

  7. window.parent与window.opener的区别

    有这样一个需求,弹出一个新窗口 并从该新页面的select选择框中选择需要的类别,再返回到之前的父窗口页面的某个文本框中.这里就要用到window.parent和window.opener 如题两种方 ...

  8. 通过xib自定义UITableViewCell

    通过xib自定义UITableViewCell 一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是pro ...

  9. resin-pro-4.0.34 服務器在windows环境下的配置

    resin-pro-4.0.34 服務器在windows环境下的配置(轉載请注明作者:icelong) 到caucho網站上http://www.caucho.com/download/下載resin ...

  10. 对比AutoResetEvent和ManualResetEvent

    ManualResetEvent和AutoResetEvent 比较 ManualResetEvent和AutoResetEvent都继承自EventWaitHandler,它们的唯一区别就在于父类 ...