python 日期操作類(lèi)代碼
完整代碼
# -*- coding: utf-8 -*-
'''獲取當(dāng)前日期前后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):
'''''
獲取當(dāng)前日期前后N月的日期
if n>0, 獲取當(dāng)前日期前N月的日期
if n<0, 獲取當(dāng)前日期后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)
這篇關(guān)于python 日期操作類(lèi)的文章就介紹到這,里面涉及了python日期操作的一些基礎(chǔ)知識(shí)。
- Python實(shí)現(xiàn)按當(dāng)前日期(年、月、日)創(chuàng)建多級(jí)目錄的方法
- python 輸出上個(gè)月的月末日期實(shí)例
- python3獲取兩個(gè)日期之間所有日期,以及比較大小的實(shí)例
- python時(shí)間日期函數(shù)與利用pandas進(jìn)行時(shí)間序列處理詳解
- python2.7 json 轉(zhuǎn)換日期的處理的示例
- Python實(shí)現(xiàn)生成隨機(jī)日期字符串的方法示例
- Python實(shí)現(xiàn)自動(dòng)為照片添加日期并分類(lèi)的方法
- Python實(shí)現(xiàn)獲取照片拍攝日期并重命名的方法
- Python SQLite3數(shù)據(jù)庫(kù)日期與時(shí)間常見(jiàn)函數(shù)用法分析
- 利用python獲取當(dāng)前日期前后N天或N月日期的方法示例
- python 計(jì)算兩個(gè)日期相差多少個(gè)月實(shí)例代碼
- Python中的日期時(shí)間處理詳解
相關(guān)文章
Python中的高級(jí)函數(shù)map/reduce使用實(shí)例
這篇文章主要介紹了Python中的高級(jí)函數(shù)map/reduce使用實(shí)例,Python內(nèi)建了map()和reduce()函數(shù),本文就講解如何使用它,需要的朋友可以參考下2015-04-04
Python實(shí)現(xiàn)批量識(shí)別圖片文字并存為Excel
批量文字識(shí)別是Python辦公自動(dòng)化的基本操作,應(yīng)用在我們工作生活中的方方面面。本文主要以開(kāi)源免費(fèi)的easyocr來(lái)實(shí)現(xiàn)批量識(shí)別圖片文字并存為Excel,感興趣的可以學(xué)習(xí)一下2022-06-06
使用Python的Flask框架實(shí)現(xiàn)視頻的流媒體傳輸
這篇文章主要介紹了使用Python的Flask框架實(shí)現(xiàn)視頻的流媒體傳輸,包括從攝像機(jī)獲取幀到web瀏覽器的數(shù)字流傳輸,需要的朋友可以參考下2015-03-03
工程師必須了解的LRU緩存淘汰算法以及python實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了工程師必須了解的LRU緩存淘汰算法以及python實(shí)現(xiàn)過(guò)程,幫助大家更好的學(xué)習(xí)算法數(shù)據(jù)結(jié)構(gòu),感興趣的朋友可以了解下2020-10-10
python cv2在驗(yàn)證碼識(shí)別中應(yīng)用實(shí)例解析
這篇文章主要介紹了python cv2在驗(yàn)證碼識(shí)別中應(yīng)用實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Django數(shù)據(jù)庫(kù)類(lèi)庫(kù)MySQLdb使用詳解
Django項(xiàng)目要操作數(shù)據(jù)庫(kù),首先要和數(shù)據(jù)庫(kù)建立連接,才能讓程序中的數(shù)據(jù)和數(shù)據(jù)庫(kù)關(guān)聯(lián)起來(lái)進(jìn)行數(shù)據(jù)的增刪改查操作。這篇文章主要介紹了Django數(shù)據(jù)庫(kù)類(lèi)庫(kù)MySQLdb使用詳解,感興趣的小伙伴們可以參考一下2019-04-04
一篇文章搞懂Python Unittest測(cè)試方法的執(zhí)行順序
unittest是Python標(biāo)準(zhǔn)庫(kù)自帶的單元測(cè)試框架,是Python版本的JUnit,下面這篇文章主要給大家介紹了如何通過(guò)一篇文章搞懂Python Unittest測(cè)試方法的執(zhí)行順序,需要的朋友可以參考下2021-09-09
Python OpenCV實(shí)現(xiàn)裁剪并保存圖片
這篇文章主要為大家詳細(xì)介紹了Python OpenCV實(shí)現(xiàn)裁剪并保存圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07

