python中計算上個月和下個月的第一天的方法


python中計算上個月和下個月的第一天的方法

 

import datetime,time
def get_1st_of_last_month():
    """
    獲取上個月第一天的日期,然后加21天就是22號的日期
    :return: 返回日期
    """
    today=datetime.datetime.today()
    year=today.year
    month=today.month
    if month==1:
        month=12
        year-=1
    else:
        month-=1
    res=datetime.datetime(year,month,1)+datetime.timedelta(days=21)
    return res

#############################

def get_1st_of_next_month():
    """
    獲取下個月的22號的日期
    :return: 返回日期
    """
    today=datetime.datetime.today()
    year=today.year
    month=today.month
    if month==12:
        month=1
        year+=1
    else:
        month+=1
    res=datetime.datetime(year,month,1)+datetime.timedelta(days=21)
    return res

#############################

def time_s_to_stamp(args):
    """
    將datetime日期格式,先timetuple()轉化為struct_time格式
    然后time.mktime轉化為時間戳
    :param args:    datetime時間格式數據
    :return:    時間戳格式數據
    """
    res=time.mktime(args.timetuple())
    return res

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM