mongona

mongona
-- --
正在获取天气

python 时间操作

function 1 ,获取指定年份间隔 以每月划分的月list

import copy
import calendar
from dateutil.relativedelta import relativedelta


def get_time_list():
    time_create = '2018-10-01 00:00:00'  # TODO
    time_distance = '2018-10-31 23:59:59'  # TODO
    time_end = '2019-08-31 23:59:59'
    start = datetime.datetime.strptime(time_create, "%Y-%m-%d %H:%M:%S")
    delatime = datetime.datetime.strptime(time_distance, "%Y-%m-%d %H:%M:%S")
    end_time = datetime.datetime.strptime(time_end, "%Y-%m-%d %H:%M:%S")
    time_list = [[start, delatime]]
    while start < end_time:
        start += relativedelta(months=1)
        delatime += relativedelta(months=1)
        end = start + datetime.timedelta(days=calendar.monthrange(start.year, start.month)[1] - 1, hours=23, minutes=59,seconds=59)
        end = copy.deepcopy(end)
        time_list.append([start, end])
    res = time_list[:-2]
    # print(res)
    return res

function 2 ,获取指定月份 以每周划分的周list

def get_week_time_list(start, end):
    week_time = list()
    temp = start
    for _ in range(4):
        start += datetime.timedelta(days=7)
        if (end - start).days < 5:
            start = end
        week_time.append([temp, start])
        temp = start
    # print(week_time)
    return week_time

 

1
0
富强,民主,文明,和谐,自由,平等,公正,法治,爱国,敬业,诚信,友善。
打赏二维码
About
Sato
毕竟,代码只是思想的一种体现而已!!! 架构师就像军师,不是对面啥阵势都用大军队来干,小阵势小技术,小公司不必要也不用引入分布式
Category
Tags
Site statistics

本站现有文章18篇,共被浏览5125

本次响应耗时: 0.058s

当前来路IP: 3.229.122.219  美国

您是本站第: 34550 位访客!

本站已苟活: 

All hots
Article archiving