Python内置模块-time-date time

1、路径

dir()的__file__ 方法

os.path.dirname(__file__)  #获取当前路径上一层

os.path.abspath(__file__)  #获取当前绝对路径

sys.path.append()  #增加到环境变量

2、time模块

 

3、datetime

t = datetime.datetime.now() #返回当前时间

t.timestamp()  #时间戳 

t.today() #返回时间元祖 datetime.datetime(2018, 7, 29, 23, 48, 29, 860069)

t.year  #返回年 2018

t.timetuple()  #返回时间对象 

datetime.date.fromtimestamp(123123123) #把一个时间戳专为datetime时间类型 datetime.date(1973, 11, 26)

t1 = datetime.timedelta(hours=3) #时间运算,括号参数默认年,year=4 hours=4

t-t1  # 回到三小时前

时间替换

>>> d = datetime.datetime.now()

>>> d

datetime.datetime(2018, 7, 29, 23, 56, 46, 605826)

>>> d.replace(year=1988,month=2,day=10)

datetime.datetime(1988, 2, 10, 23, 56, 46, 605826)

 

posted @ 2018-07-29 23:57  桑铎是大叔  阅读(116)  评论(0)    收藏  举报