黑松山资源网 Design By www.paidiu.com
前言
最近因为工作原因,发现一个Python的时间组件,很好用分享出来!(忘记作者名字了,在这里先感谢了),下面话不多说,来一起看看详细的介绍吧。
示例代码:
# -*- coding: utf-8 -*- '''获取当前日期前后N天或N月的日期''' from time import strftime, localtime from datetime import timedelta, date import calendar year = strftime("%Y", localtime()) mon = strftime("%m", localtime()) day = strftime("%d", localtime()) hour = strftime("%H", localtime()) min = strftime("%M", localtime()) sec = strftime("%S", localtime()) def today(): ''''' get today,date format="YYYY-MM-DD" ''''' return date.today() def todaystr(): ''' get date string, date format="YYYYMMDD" ''' return year + mon + day def datetime(): ''''' get datetime,format="YYYY-MM-DD HH:MM:SS" ''' return strftime("%Y-%m-%d %H:%M:%S", localtime()) def datetimestr(): ''''' get datetime string date format="YYYYMMDDHHMMSS" ''' return year + mon + day + hour + min + sec def get_day_of_day(n=0): ''''' if n>=0,date is larger than today if n<0,date is less than today date format = "YYYY-MM-DD" ''' if (n < 0): n = abs(n) return date.today() - timedelta(days=n) else: return date.today() + timedelta(days=n) def get_days_of_month(year, mon): ''''' get days of month ''' return calendar.monthrange(year, mon)[1] def get_firstday_of_month(year, mon): ''''' get the first day of month date format = "YYYY-MM-DD" ''' days = "01" if (int(mon) < 10): mon = "0" + str(int(mon)) arr = (year, mon, days) return "-".join("%s" % i for i in arr) def get_lastday_of_month(year, mon): ''''' get the last day of month date format = "YYYY-MM-DD" ''' days = calendar.monthrange(year, mon)[1] mon = addzero(mon) arr = (year, mon, days) return "-".join("%s" % i for i in arr) def get_firstday_month(n=0): ''''' get the first day of month from today n is how many months ''' (y, m, d) = getyearandmonth(n) d = "01" arr = (y, m, d) return "-".join("%s" % i for i in arr) def get_lastday_month(n=0): ''''' get the last day of month from today n is how many months ''' return "-".join("%s" % i for i in getyearandmonth(n)) def getyearandmonth(n=0): ''''' get the year,month,days from today befor or after n months ''' thisyear = int(year) thismon = int(mon) totalmon = thismon + n if (n >= 0): if (totalmon <= 12): days = str(get_days_of_month(thisyear, totalmon)) totalmon = addzero(totalmon) return (year, totalmon, days) else: i = totalmon / 12 j = totalmon % 12 if (j == 0): i -= 1 j = 12 thisyear += i days = str(get_days_of_month(thisyear, j)) j = addzero(j) return (str(thisyear), str(j), days) else: if ((totalmon > 0) and (totalmon < 12)): days = str(get_days_of_month(thisyear, totalmon)) totalmon = addzero(totalmon) return (year, totalmon, days) else: i = totalmon / 12 j = totalmon % 12 if (j == 0): i -= 1 j = 12 thisyear += i days = str(get_days_of_month(thisyear, j)) j = addzero(j) return (str(thisyear), str(j), days) def addzero(n): ''''' add 0 before 0-9 return 01-09 ''' nabs = abs(int(n)) if (nabs < 10): return "0" + str(nabs) else: return nabs def get_today_month(n=0): ''''' 获取当前日期前后N月的日期 if n>0, 获取当前日期前N月的日期 if n<0, 获取当前日期后N月的日期 date format = "YYYY-MM-DD" ''' (y, m, d) = getyearandmonth(n) arr = (y, m, d) if (int(day) < int(d)): arr = (y, m, day) return "-".join("%s" % i for i in arr) if __name__ == "__main__": print today() print todaystr() print datetime() print datetimestr() print get_day_of_day(20) print get_day_of_day(-3) print get_today_month(-3) print get_today_month(3) print get_today_month(19)
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- 【原神】V5.0攻略 | 林尼攻略一图流
- 李翊君.1993-相思的烈酒【上华】【WAV+CUE】
- 古巨基.1998-LEO.KU(国)【千禧年代】【WAV+CUE】
- 郭子.2001-原来你什么都不想要创作集丫滚石】【WAV+CUE】
- 《使命召唤:黑色行动6》新预告公布!10月25日发售
- Atlus《暗喻幻想》媒体评分汇总:高分好评如潮!
- 2024金摇杆奖提名揭晓 《黑神话》角逐最佳视觉设计!
- 群星《新说唱2024 第3期 (上)》[320K/MP3][32.76MB]
- 群星《新说唱2024 第3期 (上)》[FLAC/分轨][95.38MB]
- 群星《新说唱2024 第3期 (下)》[320K/MP3][31.36MB]
- 幻兽帕鲁手游什么时候正式上线 最新消息一览
- 西普大陆BOSS位置盘点 解锁天启纪元玩法
- 西普大陆精灵进阶培养攻略 精灵养成指南
- dnf手游法控法系职业哪个强 dnf手游法控法系职业强度排行
- 魔兽世界血藤护目镜图纸在哪买 wlk血藤护目镜图纸购买位置介绍