python 常用 time, datetime处理

python 中 time 有三种格式:

float,

struct tuple(time.struct_time 或 datetime.datetime),

str

 

常用的:

float --> struct tuple:   time.localtime( float )

struct time tuple --> str: time.strftime(format, struct time tuple)

str --> struct time tuple: time.strptime(str, format)

struct time tuple --> float : time.mktime(struct time tuple)

struct time tuple --> datetime: datetime(*time_tuple[0:6])

 

float --> datetime: datetime.datetime.fromtimestamp( float )

datetime --> str: datetime.strftime(format, datetime)

str --> datetime: datetime.strptime(str, format)

datetime --> struct time tuple: datetime.timetuple()

 

Note:

time 是 float 为基础,小数点后是毫秒,整数部分是秒。(Java 是毫秒,所以,python_time*1000 == Java_time)

datetime 是int, 略去了毫秒部分。datetime tuple 少于 struct_time

 

1. 当前时间

>>> import time
>>> time.time()
1450681042.751

>>> time.localtime(time.time())
time.struct_time(tm_year=2015, tm_mon=12, tm_mday=21, tm_hour=15, tm_min=0, tm_sec=2, tm_wday=0, tm_yday=355, tm_isdst=0)

>>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
'2015-12-21 15:01:28'


>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2015, 12, 21, 14, 58, 38, 279000)

>>> datetime.today()
datetime.datetime(2015, 12, 21, 14, 59, 20, 204000)

>>> now = datetime.now()
>>> now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond

>>> now.isocalendar() # 2015年 第52周 星期一
(2015, 52, 1)

>>> now.isoweekday() # 星期几,1:Monday; 而 now.weekday() 返回值从0开始
1

 

2. 日期字符串--> 日期

>>> s='2015-12-21 15:01:28'
>>> timeTuple = datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
datetime.datetime(2015, 12, 21, 15, 1, 28)

>>> datetime.datetime.strftime('%Y/%m/%d %H:%M:%S', timeTuple)

>>> s='2015-12-21 15:01:28'
>>> timeTuple = datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2015, tm_mon=12, tm_mday=21, tm_hour=15, tm_min=1, tm_sec=28, tm_wday=0, tm_yday=355, tm_isdst=-1)

>>> time.strftime('%Y/%m/%d %H:%M:%S',timeTuple)
  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 当前时区的名称
  %% %号本身 

 

3. 时间戳

>>> time.mktime(time.strptime(s,'%Y-%m-%d %H:%M:%S'))
1450681288.0

>>> int(time.time())
# timestamp to time tuple in UTC
timestamp = 1226527167.595983
time_tuple = time.gmtime(timestamp)
print repr(time_tuple)
# timestamp to time tuple in local time
timestamp = 1226527167.595983
time_tuple = time.localtime(timestamp)
print repr(time_tuple)

 

4. 日期相加减

import datetime

now = datetime.datetime.now() # datetime.datetime(2015, 12, 16, 15, 6, 37, 420000)
dayOfweek = datetime.datetime.isoweekday()
if dayOfweek == 1: # Monday
    last_time = now + datetime.timedelta(days=-3)
else:
    last_time = now + datetime.timedelta(days=-1)

time_delta = last_time - now
print(time_delta.days) # -1 或 -3

 

import arrow
t1 = arrow.get('2019-08-12')
t2 = arrow.get('2019-08-15')
days = (t2.date() - t1.date()).days # 3

 

5. 获取某月天数

 # 获取当月第一天的星期和当月的总天数
    firstDayWeekDay, monthRange = calendar.monthrange(year, month)

 

6. arrow的用法

import arrow

# 当前时间
>>> arrow.get()
<Arrow [2019-10-30T08:22:36.835501+00:00]>
>>> arrow.now()
<Arrow [2019-10-30T16:22:41.058720+08:00]>
>>>

>>>s1 = '2019-10-28T02:56:09.000+0000'
# 将字符串转换为timestamp
>>> arrow.get(s1).timestamp
1572231369

# 转换时区
>>>_tz = 'Asia/Shanghai'
>>>arrow.get(s1).to(_tz)
<Arrow [2019-10-28T10:56:09+08:00]>

# 转字符串
>>> arrow.get(s1).to(_tz).format('YYYY-MM-DD HH:mm:ss')
'2019-10-28 10:56:09'
>>> arrow.get(s1).to(_tz).format('YYYY-MM-DD HH:mm:ss ZZ')
'2019-10-28 10:56:09 +08:00'

# 获取delta时间之后(之前)
>>>start = arrow.now() # or arrow.get('2019-10-28 10:56:09 +08:00')
>>>end = start.shift(days=1,minutes=1439)
>>>before =  start.shift(days=-1)

 

posted @ 2015-12-21 15:51  道以万计  阅读(101253)  评论(0编辑  收藏  举报