最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python 日期操作類代碼

 更新時間:2018年05月05日 13:10:13   作者:左眼神威  
這篇文章主要介紹了python 日期操作類代碼,里面涉及了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)

這篇關于python 日期操作類的文章就介紹到這,里面涉及了python日期操作的一些基礎知識。

相關文章

  • Python中的高級函數(shù)map/reduce使用實例

    Python中的高級函數(shù)map/reduce使用實例

    這篇文章主要介紹了Python中的高級函數(shù)map/reduce使用實例,Python內建了map()和reduce()函數(shù),本文就講解如何使用它,需要的朋友可以參考下
    2015-04-04
  • Python實現(xiàn)批量識別圖片文字并存為Excel

    Python實現(xiàn)批量識別圖片文字并存為Excel

    批量文字識別是Python辦公自動化的基本操作,應用在我們工作生活中的方方面面。本文主要以開源免費的easyocr來實現(xiàn)批量識別圖片文字并存為Excel,感興趣的可以學習一下
    2022-06-06
  • 使用Python的Flask框架實現(xiàn)視頻的流媒體傳輸

    使用Python的Flask框架實現(xiàn)視頻的流媒體傳輸

    這篇文章主要介紹了使用Python的Flask框架實現(xiàn)視頻的流媒體傳輸,包括從攝像機獲取幀到web瀏覽器的數(shù)字流傳輸,需要的朋友可以參考下
    2015-03-03
  • 工程師必須了解的LRU緩存淘汰算法以及python實現(xiàn)過程

    工程師必須了解的LRU緩存淘汰算法以及python實現(xiàn)過程

    這篇文章主要介紹了工程師必須了解的LRU緩存淘汰算法以及python實現(xiàn)過程,幫助大家更好的學習算法數(shù)據結構,感興趣的朋友可以了解下
    2020-10-10
  • Python使用gensim計算文檔相似性

    Python使用gensim計算文檔相似性

    在文本處理中,比如商品評論挖掘,有時需要了解每個評論分別和商品的描述之間的相似度,以此衡量評論的客觀性。那么python 里面有計算文本相似度的程序包嗎,恭喜你,不僅有,而且很好很強大。下面我們就來體驗下gensim的強大
    2016-04-04
  • python cv2在驗證碼識別中應用實例解析

    python cv2在驗證碼識別中應用實例解析

    這篇文章主要介紹了python cv2在驗證碼識別中應用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Django數(shù)據庫類庫MySQLdb使用詳解

    Django數(shù)據庫類庫MySQLdb使用詳解

    Django項目要操作數(shù)據庫,首先要和數(shù)據庫建立連接,才能讓程序中的數(shù)據和數(shù)據庫關聯(lián)起來進行數(shù)據的增刪改查操作。這篇文章主要介紹了Django數(shù)據庫類庫MySQLdb使用詳解,感興趣的小伙伴們可以參考一下
    2019-04-04
  • 一篇文章搞懂Python Unittest測試方法的執(zhí)行順序

    一篇文章搞懂Python Unittest測試方法的執(zhí)行順序

    unittest是Python標準庫自帶的單元測試框架,是Python版本的JUnit,下面這篇文章主要給大家介紹了如何通過一篇文章搞懂Python Unittest測試方法的執(zhí)行順序,需要的朋友可以參考下
    2021-09-09
  • Python OpenCV實現(xiàn)裁剪并保存圖片

    Python OpenCV實現(xiàn)裁剪并保存圖片

    這篇文章主要為大家詳細介紹了Python OpenCV實現(xiàn)裁剪并保存圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • python實現(xiàn)歸并排序算法

    python實現(xiàn)歸并排序算法

    這篇文章主要為大家詳細介紹了Python實現(xiàn)歸并排序算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11

最新評論

虎林市| 巴塘县| 巴林左旗| 仁寿县| 沙雅县| 台安县| 盐城市| 普格县| 定襄县| 定襄县| 鱼台县| 平阴县| 沧源| 呼和浩特市| 玛曲县| 定西市| 广水市| 内丘县| 罗定市| 怀安县| 泸水县| 灌南县| 黄平县| 赤城县| 方正县| 郓城县| 贡嘎县| 罗平县| 南投市| 阜新| 天全县| 景泰县| 含山县| 阳山县| 平南县| 广河县| 甘孜县| 长宁区| 湖州市| 奇台县| 铜梁县|