zoukankan      html  css  js  c++  java
  • Python3 日期相关

    import datetime
    import time
    #获取当前日期,格式化 date0=datetime.date.today().strftime('%Y%m%d %H:%M:%S') print(date0)#只有年月日 例如:20180927 00:00:00 date1=datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S') print(date1) #例如:2018-09-27 15:21:46 date2=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') print(date2)#例如:2018-09-27 15:21:46 #日期减N天 print(datetime.date(2018,9,22)-datetime.timedelta(days=3) ) #某个日期前三天日期 #时间戳,转为日期,然后再格式化 timeStamp = 1381419600 dateArray = datetime.datetime.utcfromtimestamp(timeStamp) #时间戳,转为日期 print(dateArray.strftime('%Y%m%d-%H%M%S')) #获取时间戳 print(time.time())#原始时间时间戳 print(int(time.time()))#秒级时间戳 print(int(round(time.time()*1000))) #毫秒级时间戳

    #将指定时间设置为秒级时间戳 dt = '2019-01-01 10:40:30' ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S"))) print (ts)
    #将任意格式日期转为元组 timeArray = time.strptime('1999-10-12 12:09:23', "%Y-%m-%d %H:%M:%S") print(timeArray)#time.struct_time(tm_year=1999, tm_mon=10, tm_mday=12, tm_hour=12, tm_min=9, tm_sec=23, tm_wday=1, tm_yday=285, tm_isdst=-1)
    #
    简写方式 otherStyleTime = time.strftime("%F %A %a", timeArray) print(otherStyleTime) #例如:获取任意日期上周一到周五 def lastWeekDays(date): #'yyyy-MM-dd' list=[] if date: dateArray=time.strptime(date,'%Y-%m-%d') weekDay=dateArray.tm_wday+1#当前日期星期几 print(weekDay) #上周日日期 date=datetime.date(year=dateArray.tm_year,month=dateArray.tm_mon,day=dateArray.tm_mday) # print('上周日:%s'% (date -datetime.timedelta(days=(weekDay)))) # print('上周六:%s' % (date - datetime.timedelta(days=(weekDay+1)))) # print('上周五:%s' % (date - datetime.timedelta(days=(weekDay +2)))) # print('上周四:%s' % (date - datetime.timedelta(days=(weekDay + 3)))) # print('上周三:%s' % (date - datetime.timedelta(days=(weekDay + 4)))) # print('上周二:%s' % (date - datetime.timedelta(days=(weekDay + 5)))) # print('上周一:%s' % (date - datetime.timedelta(days=(weekDay + 5)))) date=date - datetime.timedelta(days=(weekDay)) #上周日 list.append(date) for i in range(1,7,1): date=date-datetime.timedelta(1)#上周六..周一 list.append(date) list.reverse(); return list #调用上面方法 days=lastWeekDays('2018-09-20') for i in days: print(i)
  • 相关阅读:
    抓取用户openid
    getaccesstoken方法
    https_request请求接口返回数据
    composer安装
    Linux下mysql数据库root无法登录的情况
    nginx安装
    shell脚本采用crontab定时备份数据库日志
    Python之元类详细解析
    Python面向对象之封装、property特性、绑定方法与非绑定方法
    Python之面向对象的组合、多态、菱形问题、子类中重用父类的两种方式
  • 原文地址:https://www.cnblogs.com/ygzhaof/p/9713413.html
Copyright © 2011-2022 走看看