个人知识管理站

分享个人生活、工作、学习过程中各种所学、所见、所闻,有趣的、好玩的、技术积累等各方面的内容。

您尚未登录。

公告

不积跬步无以至千里,不积小流无以成江海,网址:www.itecfun.com

#1 2015-02-05 15:14:15

xuyg
管理员
注册时间: 2015-01-21
帖子: 667

Python获取当前时间

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  当前时区的名称
%%  %号本身

离线

#2 2015-02-05 15:24:06

xuyg
管理员
注册时间: 2015-01-21
帖子: 667

Re: Python获取当前时间

Python:如何获取当前的日期和时间


在python里如何获取当前的日期和时间呢?在Python语言里,我们可以通过调用什么模块或者类函数来得到当前的时间或日期呢?

当然你可以使用时间模块(time module),该模块提供了各种和时间相关的函数。但是这个模块里的一些函数在某些平台里不可用。那么怎么办呢?我们
可以使用一个更高级的面向对象的接口函数:datetime。它提供了操作日期和时间的多种简单或复杂的方法。

python里使用time模块来获取当前的时间

1.time.strftime(format)
2.time.strftime("%H:%M:%S") ##24小时格式
3.time.strftime("%I:%M:%S")## 12小时格式

示例
一个获取当天日期和时间的简单python程序

#!/usr/bin/python

import time
print (time.strftime("%H:%M:%S"))

## 12 hour format ##
print (time.strftime("%I:%M:%S"))

示例输出:

18:11:30
6:11:30

打印出当前的日期的python程序

#!/usr/bin/python

import time
## dd/mm/yyyy格式
print (time.strftime("%d/%m/%Y"))

示例输出:

11/10/2013

格式参数:(以下蓝色字体在Python 2.7.6中没有错误,3.0及以上版本中没测试过)


%a 星期几的简写
%A 星期几的全称
%b 月分的简写
%B 月份的全称
%c 标准的日期的时间串
%C 年份的后两位数字      Invalid format string
%d 十进制表示的每月的第几天
%D 月/天/年                    Invalid format string
%e 在两字符域中,十进制表示的每月的第几天  Invalid format string
%F 年-月-日    Invalid format string
%g 年份的后两位数字,使用基于周的年   Invalid format string
%G 年分,使用基于周的年  Invalid format string
%h 简写的月份名  Invalid format string

%H 24小时制的小时
%I 12小时制的小时
%j 十进制表示的每年的第几天
%m 十进制表示的月份
%M 十时制表示的分钟数
%n 新行符  Invalid format string   
%p 本地的AM或PM的等价显示
%r 12小时的时间 Invalid format string   
%R 显示小时和分钟:hh:mm   Invalid format string   

%S 十进制的秒数
%t 水平制表符                        Invalid format string   
%T 显示时分秒:hh:mm:ss      Invalid format string   
%u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)  Invalid format string     

%U 第年的第几周,把星期日做为第一天(值从0到53)
%V 每年的第几周,使用基于周的年
%w 十进制表示的星期几(值从0到6,星期天为0)
%W 每年的第几周,把星期一做为第一天(值从0到53)
%x 标准的日期串
%X 标准的时间串
%y 不带世纪的十进制年份(值从0到99)
%Y 带世纪部分的十制年份
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号

使用datetime模块来获取当前的日期和时间
参数如下:

 

cur=datetime.datetime.now()
cur.hour
cur.minute
cur.year
cur.day
cur.month

 

例子:


#!/usr/bin/python
import datetime
i = datetime.datetime.now()
print ("当前的日期和时间是 %s" % i)
print ("ISO格式的日期和时间是 %s" % i.isoformat() )
print ("当前的年份是 %s" %i.year)
print ("当前的月份是 %s" %i.month)
print ("当前的日期是  %s" %i.day)
print ("dd/mm/yyyy 格式是  %s/%s/%s" % (i.day, i.month, i.year) )
print ("当前小时是 %s" %i.hour)
print ("当前分钟是 %s" %i.minute)
print ("当前秒是  %s" %i.second)

示例输出:


当前的日期和时间 = 2013-10-11 19:38:19.4545
ISO格式的日期和时间 = 2013-10-11T19:38:19.4545
当前的年份 2013
当前的月份 10
当前的日期  11
dd/mm/yyyy 格式是  11/10/2013
当前小时是 0
当前分钟是 38
当前秒是  19

离线

#3 2015-02-05 17:36:40

xuyg
管理员
注册时间: 2015-01-21
帖子: 667

Re: Python获取当前时间

针对楼上有关于格式参数中蓝色字体的部分 错误验证的结果


>>> import datetime
>>> import time
>>> workdate='2015/02/05'
>>> time.strptime(workdate,'%Y/%m/%d')
time.struct_time(tm_year=2015, tm_mon=2, tm_mday=5, tm_hour=0, tm_min=0, tm_sec=
0, tm_wday=3, tm_yday=36, tm_isdst=-1)
>>> workday=time.strptime(workdate,'%Y/%m/%d')
>>> myworkdate=datetime.datetime(*workdate[:6])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: an integer is required
>>> dtstr='2015/02/05'
>>>
>>>
>>> t=time.strptime(dtstr,'%Y/%m/%d')
>>>
>>>
>>> workdate=datetime.datetime(*t[:6])
>>>
>>>
>>> workdate.strftime('%u')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> t.strftime('%u')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'time.struct_time' object has no attribute 'strftime'
>>>
>>>
>>> workdate.strftime('%w')
'4'
>>> workdate.strftime('%u')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>>
>>>
>>>
>>> time.strftime('%u')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>>
>>>
>>>
>>> time.strftime('%a')
'Thu'
>>> time.strftime('%A')
'Thursday'
>>>
>>>
>>>
>>> time.strftime('%b')
'Feb'
>>>
>>>
>>> time.strftime('%B')
'February'
>>>
>>>
>>> time.strftime('%c')
'02/05/15 16:18:56'
>>>
>>>
>>> time.strftime('%C')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>>
>>>
>>> datetime.strftime('%C')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'module' object has no attribute 'strftime'
>>> time.strftime('%C')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>>
>>>
>>>
>>>
>>>
>>> time.strftime('%n')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime('%U')
'05'
>>> time.strftime('%C')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>>
>>>
>>>
>>> time.strftime('%d')
'05'
>>> time.strftime('%D')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime('%e')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime('%F')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>>
>>>
>>>
>>> time.strftime('%g')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime('%G')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime('%h')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime('%H')
'16'
>>> time.strftime('%I')
'04'
>>> time.strftime('%j')
'036'
>>> time.strftime('%m')
'02'
>>> time.strftime('%M')
'23'
>>> time.strftime('%n')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime('%p')
'PM'
>>> time.strftime('%r')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>>
>>> time.strftime('%R')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime('%S')
'52'
>>> time.strftime('%t')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime('%T')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime('%u')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime('%U')
'05'
>>> time.strftime('%V')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime('%w')
'4'
>>> time.strftime('%W')
'05'
>>> time.strftime('%x')
'02/05/15'
>>> time.strftime('%X')
'16:25:42'
>>> time.strftime('%y')
'15'
>>> time.strftime('%Y')
'2015'
>>> time.strftime('%z')
'\xd6\xd0\xb9\xfa\xb1\xea\xd7\xbc\xca\xb1\xbc\xe4'
>>> time.strftime('%Z')
'\xd6\xd0\xb9\xfa\xb1\xea\xd7\xbc\xca\xb1\xbc\xe4'
>>> time.strftime('%%')
'%'
>>>

离线

页脚

©2019 YG Wang 备案号: 赣ICP备19010196号-1