1. os.path中的st_ctime、 st_mtime和st_atime
python os.stat中 st_ctime 在windows系统可以用来获取文件的创建时间,在linux系统没有创建时间的概念,
st_ctime--文件状态修改时间。Time when file status was last changed. Changed by the following functions:
chmod(), chown(), create(), link(2), mknod(), pipe(), unlink(2), utime(), and write().
st_mtime--文件内容修改时间。Time when data was last modified. Changed by the following functions:
create(), mknod(), pipe(), utime(), and write(2).
st_ atime--文件访问时间。 Time when file data was last accessed. Changed by thefollowing functions:
create(), mknod(), pipe(), utime(2), and read(2).
st_ctime、st_mtime、st_atime都返回时间戳
2. os.utime()
# 修改访问和修改时间, 文件为绝对路径,访问和修改时间入参都是时间戳
os.utime(文件绝对路径, (1330712280, 1330712292))
os.utime(文件绝对路径, None) #修改访问和修改时间为当前时间
3.获取当前时间、日期
获取当前时间的时间戳: time.time()
格式化输出当前日期为字符串,如20171228 ,用time.strftime("%Y%m%d")
或者
timeArray = time.localtime(timestamp) #timestamp为时间戳入参
date = time.strftime("%Y%m%d", timeArray)
或者(首选)
time.strftime("%Y%m%d", time.localtime())
格式化输出当前时间为字符串,如20171228 14:40:40
timeArray = time.localtime(timestamp) #timestamp为时间戳入参
dateAndTime = time.strftime("%Y%m%d %H:%M:%S", timeArray)
或者(首选):
time.strftime("%Y%m%d %H:%M:%S", time.localtime())
4. 获取下一个工作日(股票交易日T-->T+1)
def get_nextworkday():
# python中用%w格式化输出星期几, 星期分别对应字符0-6, '0'对应星期天,'1'对应星期一,'5'对应星期五
week = time.strftime("%w", time.localtime())
#星期五对应'5', 下一个工作是为当前日期加3天
if week == '5':
nextworkday = (datetime.datetime.now() + datetime.timedelta(days=3)).strftime("%m/%d/%Y")
#星期六对应'6', 下一个工作是为当前日期加2天
elif week == '6':
nextworkday = (datetime.datetime.now() + datetime.timedelta(days=2)).strftime("%m/%d/%Y")
else:
nextworkday = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%m/%d/%Y")
return nextworkday
5. 字符串转为日期
str = '2020-01-01'
date = datetime.datetime.strptime(str,'%Y-%m-%d')
6.日期转为字符串
与格式化输出日期相同
datetime.now().strftime('%Y-%m-%d')
7. 从字符串中匹配日期,并格式化为另一种日期格式
text = 'this is a sunny day, in 2018-12-01, winter come.'
match = re.search(r'(d{4}-d{2}-d{2})', text)
date1 = datetime.datetime.strptime(match.group(), '%Y-%m-%d')
#在转为字符串时进行格式转化
tranferred_date = date1.strftime("%m/%d/%Y")
8.获取指定日期上个月最后一天
涉及月份和年份问题,用指定日期的月初减去一天
str = '01/01/2020'
date = datetime.datetime.strptime(str, '%m/%d/%Y')
last_day_of_previos_month = (datetime.date(date.year, date.month, 1) - datetime.timedelta(1)).strftime("%m/%d/%Y")
9.excel 数字日期转为标准日期
#数字日期number_date必须为int类型, 需要import xlrd
number_date = 43404
standard_date = (xlrd.xldate.xldate_as_datetime(number_date, datemode = 0)).strftime("%m/%d/%Y")
10. 时间字符串转为时间戳
date_str = '06/01/2018'
date = datetime.datetime.strptime(date_str, '%m/%d/%Y')
timestamp = time.mktime(date.timetuple())
11. 时间字符串转为星期
#返回0-6, 0为周日, 1为周一,6为周六
date_str = '06/01/2018'
date = datetime.datetime.strptime(date_str, '%m/%d/%Y')
week = time.strftime("%w", date.timetuple())
12.获取给定日期字符串的前一天
date_str = '06/01/2018'
date = datetime.datetime.strptime(date_str, '%m/%d/%Y')
lastday =(date + datetime.timedelta(days=-1)).strftime("%m/%d/%Y")
13.获取日期字符串的年份
date_str = '06/01/2018'
date = datetime.datetime.strptime(date_str, '%m/%d/%Y')
yr = date.year
14. 用给定的年、月、日组成日期
date_str = '06/01/2018'
date = datetime.datetime.strptime(date_str, '%m/%d/%Y')
yr = date.year
t = (datetime.datetime(yr, 9, 23)).strftime('%m/%d/%Y')
15.时间的大小比较
时间元组可以进行大小比较
date_str = '06/01/2018'
date = datetime.datetime.strptime(date_str, '%m/%d/%Y')
date2 = datetime.datetime(2017, 3, 31)
if date>date2:
...
16.年份减1
# q的返回2017
date_str = '06/01/2018'
date = datetime.datetime.strptime(date_str, '%m/%d/%Y')
yr = date.year
q = yr - 1
17. datetime获取当前时间,获取年、月、日
cur = datetime.datetime.now()
print(cur.month)
print(cur.year)
print(cur.minute)
18.时区转换
import pytz
# 打印美国时区
print(pytz.country_timezones('us'))
tz = pytz.timezone('America/New_York')
ny_time = datetime.datetime.now(tz).strftime("%m/%d/%Y")
#使用美国时区,获取星期、日期、年份
a = datetime.datetime.now(pytz.timezone('America/New_York'))
week = time.strftime("%w", a.timetuple())
date = time.strftime("%m/%d/%Y", a.timetuple())
year1 = a.year
19.去掉日期中的前导0
# eg 01/03/2019-->1/3/2019, timetuple结构体中的数字不含前导0
time = datetime.datetime.now().timetuple()
date_now = str(time.tm_mon) + '/' + str(time.tm_mday) + '/' + str(time.tm_year)