欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 日期操作類代碼

 更新時間:2018年05月05日 13:10:13   作者:左眼神威  
這篇文章主要介紹了python 日期操作類代碼,里面涉及了python日期操作的一些基礎(chǔ)知識,需要的朋友可以參考下

完整代碼

# -*- 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)

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

相關(guān)文章

  • 詳解Python的Lambda函數(shù)與排序

    詳解Python的Lambda函數(shù)與排序

    本篇文章主要是介紹了Python的Lambda函數(shù)與排序,簡單的介紹了Lambda函數(shù)的用法和排序,有需要的朋友可以了解一下。
    2016-10-10
  • Python讀寫yaml文件

    Python讀寫yaml文件

    這篇文章主要介紹了Python讀寫yaml文件,yaml?是專門用來寫配置文件的語言,非常簡潔和強大,之前用ini也能寫配置文件,有點類似于json格式,下面關(guān)于Python讀寫yaml文件的詳細資料,需要的小伙伴可以參考一下
    2022-03-03
  • Python實現(xiàn)遍歷大量表格文件并篩選出數(shù)據(jù)缺失率低的文件

    Python實現(xiàn)遍歷大量表格文件并篩選出數(shù)據(jù)缺失率低的文件

    這篇文章主要為大家詳細介紹了如何利用Python實現(xiàn)遍歷大量表格文件并篩選出表格內(nèi)數(shù)據(jù)缺失率低的文件的功能,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-05-05
  • python中update的基本使用方法詳解

    python中update的基本使用方法詳解

    這篇文章主要介紹了python中update的基本使用方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • Python處理Excel文件實例代碼

    Python處理Excel文件實例代碼

    本篇文章主要介紹了Python處理Excel文件實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 使用Pyqt5制作屏幕錄制界面功能

    使用Pyqt5制作屏幕錄制界面功能

    這篇文章主要介紹了使用Pyqt5制作屏幕錄制界面,主要介紹如何使用ffmpeg將同時錄制的屏幕錄像和音頻合成為有聲音的屏幕錄像,需要的朋友可以參考下
    2022-04-04
  • Python使用matplotlib繪制圖形大全(曲線圖、條形圖、餅圖等)

    Python使用matplotlib繪制圖形大全(曲線圖、條形圖、餅圖等)

    matplotlib 是一個用于創(chuàng)建靜態(tài)、動態(tài)和交互式可視化圖形的 Python 庫,它被廣泛用于數(shù)據(jù)可視化,并且可以與多種操作系統(tǒng)和圖形后端一起工作,本文給大家介紹了Python使用matplotlib繪制圖形大全,需要的朋友可以參考下
    2024-06-06
  • python 制作本地應(yīng)用搜索工具

    python 制作本地應(yīng)用搜索工具

    這篇文章主要介紹了python 制作本地應(yīng)用搜索工具的方法,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下
    2021-02-02
  • python Django框架實現(xiàn)web端分頁呈現(xiàn)數(shù)據(jù)

    python Django框架實現(xiàn)web端分頁呈現(xiàn)數(shù)據(jù)

    這篇文章主要介紹了python Django框架實現(xiàn)web端分頁呈現(xiàn)數(shù)據(jù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • 分享一個可以生成各種進制格式IP的小工具實例代碼

    分享一個可以生成各種進制格式IP的小工具實例代碼

    這篇文章主要給大家分享了一個可以生成各種進制格式IP的小工具,利用的語言是python實現(xiàn)的一個小工具,這個小工具對大家的日常使用與開發(fā)具有一定的參考學習價值,需要的朋友們下面跟著小編來一起看看吧。
    2017-07-07

最新評論