python获取时间格式为yyyy-MM-dd的时间工具类

通过调用下面的方法获取想要获得的时间,时间格式为yyyy-MM-dd

# encoding: utf-8
import datetime


def get_curmonth_firstday():
    """
    获取当月第一天
    :return:
    """
    return datetime.date(datetime.date.today().year,datetime.date.today().month,1)


def get_lastmonth_lastday():
    """
    获取上月最后一天
    :return:
    """
    return get_curmonth_firstday() - datetime.timedelta(days=1)


def get_lastmonth_firstday():
    """
    获取上月第一天
    :return:
    """
    return datetime.date(get_lastmonth_lastday().year, get_lastmonth_lastday().month, 1)


def get_premonth_firstday(num):
    """
    获取前几个月第一天
    :param num: 月份数
    :return:
    """
    i = 1
    day = get_curmonth_firstday()
    while(i <= num):
        day = day - datetime.timedelta(days=1)
        day = datetime.date(day.year, day.month, 1)
        i = i + 1
    return day


def get_premonth_lastday(num):
    """
    获取前几个月最后一天
    :param num: 月份数
    :return:
    """
    i = 1
    day = get_curmonth_firstday()
    while(i <= num):
        day = datetime.date(day.year, day.month, 1)
        day = day - datetime.timedelta(days=1)
        i = i + 1
    return day


def get_preday(num):
    """
    获取前几天
    :param num: 天数
    :return:
    """
    i = 1
    while(i <= num):
        today = datetime.date.today()
        day = today - datetime.timedelta(days=num)
        i = i + 1
    return day


def get_today():
    """
    获取今天
    :return:
    """
    return datetime.date.today()

def get_prespcday(spcday, num):
    """
    获取前几天
    :param num: 天数
    :return:
    """
    i = 1
    while(i <= num):
        spcday1 = datetime.datetime.strptime(spcday, "%Y-%m-%d")
        day = spcday1 - datetime.timedelta(days=num)
        i = i + 1
    return str(day).split(' ')[0]


def get_nextspcday(spcday, num):
    """
    获取前几天
    :param num: 天数
    :return:
    """
    i = 1
    while(i <= num):
        spcday1 = datetime.datetime.strptime(spcday, "%Y-%m-%d")
        day = spcday1 + datetime.timedelta(days=num)
        i = i + 1
    return str(day).split(' ')[0]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值