mg4377娱乐娱城官网_mg4377娱乐手机版_www.mg4377.com

python总括N天从此日期的章程,python获取当今天期

时间:2019-06-07 17:50来源:mg4377娱乐手机版
python总结N天之后的日子,可以友善写成3个函数,想获取几天后的日期都行 python总括N天从此日期的章程,python获取当今天期前后N天或N月的日子。python总计N天过前些天期的章程,pytho

python总结N天之后的日子,可以友善写成3个函数,想获取几天后的日期都行

python总括N天从此日期的章程,python获取当今天期前后N天或N月的日子。python总计N天过前些天期的章程,python计算n日期

正文实例讲述了python总结N天过前几天期的方式。分享给大家供我们参谋。具体如下:

python总计N天从此的日子,能够本身写成叁个函数,想赢得几天后的日期都行

#! /usr/bin/env python
#coding=utf-8
import time
import datetime
d1 = datetime.datetime.now()
d3 = d1   datetime.timedelta(days =10)
print str(d3)
print d3.ctime()

出口结果如下:

2015-02-18 16:49:28.362000
Fri Feb 18 16:49:28 2015

瞩望本文所述对大家的Python程序设计具备援救。

本文实例讲述了python总计N天过后天期的主意。分享给我们供大家参照他事他说加以调查。具体如下: 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 
            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 
            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总括日期之间的放假期期,供我们参照他事他说加以考查,具体内容如下

#! /usr/bin/env python 
#coding=utf-8 

import time 
import datetime 

d1 = datetime.datetime.now() 
d3 = d1   datetime.timedelta(days =10) 

print str(d3) 
print d3.ctime() 

 

代码如下:

编辑:mg4377娱乐手机版 本文来源:python总括N天从此日期的章程,python获取当今天期

关键词: Python python日期 python时间