python 日期操作类代码

yipeiwu_com5年前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实现的文本对比报告生成工具示例

Python实现的文本对比报告生成工具示例

本文实例讲述了Python实现的文本对比报告生成工具。分享给大家供大家参考,具体如下: 借助于difflib的功能,可以针对我们的使用情况进一步进行功能的聚合。我想要的功能是输入两个文件...

Python中的heapq模块源码详析

Python中的heapq模块源码详析

起步 这是一个相当实用的内置模块,但是很多人竟然不知道他的存在——笔者也是今天偶然看到的,哎……尽管如此,还是改变不了这个模块好用的事实 heapq 模块实现了适用于Python列表的...

Python的Flask开发框架简单上手笔记

最简单的hello world #!/usr/bin/env python # encoding: utf-8 from flask import Flask app = Fla...

Python中内置的日志模块logging用法详解

Python中内置的日志模块logging用法详解

logging模块简介 Python的logging模块提供了通用的日志系统,可以方便第三方模块或者是应用使用。这个模块提供不同的日志级别,并可以采用不同的方式记录日志,比如文件,HTT...

python简单线程和协程学习心得(分享)

python中对线程的支持的确不够,不过据说python有足够完备的异步网络框架模块,希望日后能学习到,这里就简单的对python中的线程做个总结 threading库可用来在单独的线程...