python日期
处理时间戳
import datetime
print('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
print('Timestamp: {:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()))
print('Date now: %s' % datetime.datetime.now())
print('Date today: %s' % datetime.date.today())
today = datetime.date.today()
print("Today's date is {:%b, %d %Y}".format(today))
schedule = '{:%b, %d %Y}'.format(today) + ' - 6 PM to 10 PM Pacific'
schedule2 = '{:%B, %d %Y}'.format(today) + ' - 1 PM to 6 PM Central'
print('Maintenance: %s' % schedule)
print('Maintenance: %s' % schedule2)输出:
Timestamp: 2014-10-18 21:31:12
Timestamp: 2014-Oct-18 21:31:12
Date now: 2014-10-18 21:31:12.318340
Date today: 2014-10-18
Today's date is Oct, 18 2014
Maintenance: Oct, 18 2014 - 6 PM to 10 PM Pacific
Maintenance: October, 18 2014 - 1 PM to 6 PM Central我写了一个简单的记录当时执行命令的结果加时间戳记录
unix时间戳和日期转换
使用datetime模块可以转换unix时间
输出显示 2016-10-24 14:23:59
参考 Converting unix timestamp string to readable date in Python
相反,从时间转换成日期转换成unix timestamp方法需要计算,也就是需要计算当前时间到,并且各个python版本的转换方法不同
Python 2
这里
datetime格式是datetime.datetime(2011, 1, 1, 0, 0)(参考 datetime Objects)
例如要查 2016-10-22 00:55:26 对应time stamp使用如下方式:
Python 3 (< 3.3)
Python 3.3+
文件名修改成带时间戳的后缀
判断文件大小,超过指定大小进行重命名
获取15分钟或1小时后的时间
datetime支持timedelta函数能够获取指定时间差异的时间
参考
Last updated
Was this helpful?