Python日期时间模块简单示例

作者: Jiafu | 来源:发表于2017-09-29 12:57 被阅读20次

不多说,python中用到日期、时间的话使用datetime这个模块,下面给出一些简单的用法示例,包括创建实例、格式化、由字符串创建实例、简单计算等等。

Python代码:

# -*- coding: utf8 -*-

from datetime import datetime

#get current datetime

datetime_now = datetime.now()

print "now:", datetime_now

 
#create two datetime object

datetime1 = datetime(year = 2012, month = 2, day = 28)

datetime2 = datetime(year = 2012, month = 2, day = 29, hour = 12, minute = 30, second = 30, microsecond = 1000)

print "datetime1:", datetime1, " datetime2:", datetime2

 
#format

##%a    Locale’s abbreviated weekday name.     
##%A    Locale’s full weekday name.     
##%b    Locale’s abbreviated month name.     
##%B    Locale’s full month name.     
##%c    Locale’s appropriate date and time representation.     
##%d    Day of the month as a decimal number [01,31].     
##%H    Hour (24-hour clock) as a decimal number [00,23].     
##%I    Hour (12-hour clock) as a decimal number [01,12].     
##%j    Day of the year as a decimal number [001,366].     
##%m    Month as a decimal number [01,12].     
##%M    Minute as a decimal number [00,59].     
##%p    Locale’s equivalent of either AM or PM.    (1)

##%S    Second as a decimal number [00,61].    (2)

##%U    Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.    (3)

##%w    Weekday as a decimal number [0(Sunday),6].     
##%W    Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.    (3)

##%x    Locale’s appropriate date representation.     
##%X    Locale’s appropriate time representation.     
##%y    Year without century as a decimal number [00,99].     
##%Y    Year with century as a decimal number.     
##%Z    Time zone name (no characters if no time zone exists).     
##%%    A literal '%' character.

print datetime1.strftime("%d/%m/%y")

print datetime2.strftime("%A %d. %B %Y")

 
#strptime

str_time = "Sat Mar 28 22:24:24 2009"

b = datetime.strptime(str_time, "%a %b %d %H:%M:%S %Y")

print b

 
#calculate

delta_T =  datetime2 - datetime1

print "delta_T:", delta_T, ";delta_T.seconds:", delta_T.seconds, ";delta_T.total_seconds():", delta_T.total_seconds()

print datetime1 < datetime2

输出:

now: 2012-02-29 10:42:34.625000
datetime1: 2012-02-28 00:00:00 datetime2: 2012-02-29 12:30:30.001000
28/02/12
Wednesday 29. February 2012
2009-03-28 22:24:24
delta_T: 1 day, 12:30:30.001000 ;delta_T.seconds: 45030 ;delta_T.total_seconds(): 131430.001
True

相关文章

  • Python日期时间模块简单示例

    不多说,python中用到日期、时间的话使用datetime这个模块,下面给出一些简单的用法示例,包括创建实例、格...

  • python Argparse库简单示例

    Argparse 库:python的命令行解析模块 与Linux的ls用法较类似 简单示例 示例结果: 参考:ht...

  • 2018-06-29

    python学习 学习python字符串、列表、元组、字典、日期和时间模块

  • Python Day32

    python大大的图 python使用datetime模块timedelta实现日期时间相加 计算明天的日期 把l...

  • python基础篇07-日期模块

    python内置了多个用于日期和时间进行操作的内置模块:time模块,datetime模块和calendar模块。...

  • Python 时间和日期模块

    time模块方法: time.time():获取当前时间的时间戳 时间戳转换为北京时间:http://tool.c...

  • argparse

    argparse import argparse命令行参数解析模块简单示例 选择性添加参数 python test...

  • 【Python学习笔记】时间序列

    1. Python的日期与时间工具 Python基本的日期与时间功能都在标准库的datetime模块中,例如使用d...

  • Python datetime模块参考手册

    Python提供了多个内置模块用于操作日期时间,像 calendar,time,datetime。time模块提供...

  • Python常用的内建模块1:datetime

    datetime:Python处理日期和时间的标准库 获取当前日期和时间: datetime是模块, dateti...

网友评论

    本文标题:Python日期时间模块简单示例

    本文链接:https://www.haomeiwen.com/subject/gcriextx.html