关于python时间日历的部分基础语法

本文主要介绍了Python编程中时间日历模块的基础语法,包括日期和时间的处理,日历函数的使用等核心知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import time
# 引入时间包
time_line=time.time()
# 1970年1月1日(08:00:00GMT)至当前时间的总毫秒数。它也被称为Unix时间戳,为啥是从1970自己百度
print(time_line)
#输出结果,以秒数为单位,可以换算日期


time1=time.localtime()
print(time1)
# 输出本地计算机的时间
#例如:time.struct_time(tm_year=2018, tm_mon=7, tm_mday=2, tm_hour=21, tm_min=17,
#                    tm_sec=5, tm_wday=0, tm_yday=183, tm_isdst=0)
#输出的内容分别是年月日时分秒,周几(python中周一从0开始),当年第几天和夏令时


time2=time.localtime(1530535661)
print(time2)
#获取1970开始某个制定时间的计算机秒数所对应的时间,此处以1530535661为例,输入时间的格式和上边输出的计算机时间格式相同“年月日时分秒周天夏令时
time3=time.strftime("%y-%m-%d %H:%M:%S",time.localtime())
print(time3)
#输出指定格式的时间,以-和:隔开表示,以现在时间为例 :18-07-02 21:32:03
# y  year m month d day H hours M minute S second



import datetime

date1=datetime.datetime.today()
print(date1)

date2=datetime.datetime.now()
print(date2)

#datetime.today和datetime.now 是一样的效果,都是获取即时时间,以现在结果 2018-07-02 21:50:56.310272为例

import datetime
date2=datetime.datetime.now()
date3=date2.strftime("%yyear%mmonth%dday")
print(date3.replace("year","年").replace("month","月").replace("day","天"))
#将获取的结果用中文表示,strftime不可以直接输出中文,replace=替换
# %y 获取年  %m获取月  %d 获取日


date4 = datetime.timedelta(hours=12 ,minutes= 30)
print(date4)
# 设置时间间隔
date5 = datetime.datetime.now() + date4
print(date5)
# 从现在往后 推迟指定的时间,指定的时间为date4的值


date5=  datetime.datetime.today()
date6 = date5.date()
print(date6)
# 只获取当前的日期
print('{}年{}月{}日'.format(date6.year ,date6.month ,date6.day))
#用formate表示出汉字年月日

date7 = date5.time()
print(date7)
print('{}时{}分{}秒'.format(date7.hour ,date7.minute ,date7.second))
# 获取当前时间戳
print('当前的时间戳为{}'.format(date5.timestamp()))


--------------------7-11重整简写,有点乱,反正除了我也没人看,哪天忘了还能回来查查--------------------------------------

import time
print(time.time())     #时间戳

import datetime
print(datetime.datetime.today())   #现在的时间常用的格式
print(datetime.datetime.now())

print(time.localtime())   #获取常用的时间

print(time.localtime(4582654521))  #用指定的时间戳获取对应的时间

time4 = time.strftime('%y-%m-%d %H:%M:%S',time.localtime())   #得到指定格式的时间
print(time4)

print(time.strftime('%y-%m-%d %H %M %S',time.localtime()))
#获取指定格式的时间,y年,m月,d日,H时,

date2=datetime.datetime.now()   #获取以英文格式的时间
date3=date2.strftime('%yyear%mmonth%dday')  #吧上边的时间y m d 年月日格式改为year,mouth day格式
print(date3.replace("year","年").replace("month","月").replace("day","日"))
# replace 替换,吧英文的年月日替换成年月日


print(datetime.datetime.now()+datetime.timedelta(hours=12,minutes=10))
#输出时间为现在的时间加上往后推迟固定12时10分的时间,括号内是具体的时间

# time.sleep(5)
# print("稍等片刻")
#线程休眠,适用于爬虫里边用来减缓速度,或者a代码和b代码冲突,用来优先执行其一,休眠不是个好办法
#或者定时任务,到指定时间触发执行某个任务在此之前休眠



date5=  datetime.datetime.today()
date6=date5.date()
print(date6)

print(datetime.datetime.today().date())
#上边两个效果一样的,功能是只获取日期 年月日三个时间以-隔开
print('{}年{}月{}日'.format(date6.year,date6.month,date6.day))
#用format变成汉字的年月日


date7=date5.time()
print(date7)


import datetime
print(datetime.datetime.today().time())
#只获取显示现在的时分秒




print('{}时{}分{}秒'.format(date7.hour,date7.minute,date7.second))
#yong format 显示的时分秒

print('当前时间戳是{}'.format(date5.timestamp()))












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值