python时间模块

获取当日、本周、本月、昨日、上周、上月时间

关注公众号“轻松学编程”了解更多。

import datetime


def get_current_day():
    '''
    当日
    :return:
    '''
    end_time = datetime.date.today()
    year = end_time.strftime("%Y")
    month = end_time.strftime("%m")
    day = end_time.strftime("%d")
    params = {
        "year": year,
        "month": month,
        "day": day,
        "current_date": f"{year}-{month}-{day}"
    }

    return params


def get_current_week():
    '''
    当周
    :return:
    '''
    start_time, end_time = datetime.date.today(), datetime.date.today()
    one_day = datetime.timedelta(days=1)
    while start_time.weekday() != 0:
        start_time -= one_day
    while end_time.weekday() != 6:
        end_time += one_day
    start_year = start_time.strftime("%Y")
    start_month = start_time.strftime("%m")
    start_day = start_time.strftime("%d")
    year = end_time.strftime("%Y")
    month = end_time.strftime("%m")
    end_day = end_time.strftime("%d")
    params = {
        "start_year": start_year,  # 开始年份
        "start_month": start_month,  # 开始月份
        "start_day": start_day,  # 开始日期
        "start_date": f"{start_year}-{start_month}-{start_day}",  # 开始日期
        "end_year": year,  # 结束年份
        "end_month": month,  # 结束月份
        "end_day": end_day,  # 结束日期
        "end_date": f"{year}-{month}-{end_day}"  # 结束日期
    }

    return params


def get_current_month():
    '''
    当月
    当前时间的月份首日与最后一天
    :return:
    '''
    today = datetime.date.today()
    year, month = today.year, today.month
    end = datetime.datetime(year, month + 1, 1) - datetime.timedelta(days=1)
    params = {
        "year": year,
        "month": month,
        "start_day": "01",
        "start_date": f"{year}-{month}-01",
        "end_day": f"{end.day}",
        "end_date": f"{year}-{month}-{end.day}",
    }

    return params


def get_last_day():
    '''
    昨天
    :return:
    '''
    d = datetime.date.today()
    one_day = datetime.timedelta(days=1)
    end_time = d - one_day
    year = end_time.strftime("%Y")
    month = end_time.strftime("%m")
    day = end_time.strftime("%d")
    params = {
        "year": year,
        "month": month,
        "day": day,
        "last_date": f"{year}-{month}-{day}"
    }

    return params


def get_last_week():
    '''
    上周
    :return:
    '''
    d = datetime.date.today()
    days_count = datetime.timedelta(days=d.isoweekday())
    end_time = d - days_count
    six_days = datetime.timedelta(days=6)
    start_time = end_time - six_days
    start_year = start_time.strftime("%Y")
    start_month = start_time.strftime("%m")
    start_day = start_time.strftime("%d")
    year = end_time.strftime("%Y")
    month = end_time.strftime("%m")
    end_day = end_time.strftime("%d")
    params = {
        "start_year": start_year,
        "start_month": start_month,
        "start_day": start_day,
        "start_date": f"{start_year}-{start_month}-{start_day}",
        "year": year,
        "month": month,
        "end_day": end_day,
        "end_date": f"{year}-{month}-{end_day}",
    }

    return params


# 上月
def get_last_month():
    d = datetime.date.today()
    days_count = datetime.timedelta(days=d.day)
    end_time = d - days_count
    year = end_time.strftime("%Y")
    month = end_time.strftime("%m")
    params = {
        "year": year,
        "month": month,
        "start_day": "01",
        "start_date": f"{year}-{month}-01",
        "end_day": f"{end_time.day}",
        "end_date": f"{year}-{month}-{end_time.day}",
    }

    return params


if __name__ == '__main__':
    print('当日', get_current_day())
    print('当周', get_current_week())
    print('当月', get_current_month())
    print('昨日', get_last_day())
    print('上周', get_last_week())
    print('上月', get_last_month())

后记
【后记】为了让大家能够轻松学编程,我创建了一个公众号【轻松学编程】,里面有让你快速学会编程的文章,当然也有一些干货提高你的编程水平,也有一些编程项目适合做一些课程设计等课题。

也可加我微信【1257309054】,拉你进群,大家一起交流学习。
如果文章对您有帮助,请我喝杯咖啡吧!

公众号
在这里插入图片描述

posted @ 2020-09-22 17:46  轻松学编程  阅读(109)  评论(0编辑  收藏  举报