Python之計算當前月份的日期范圍(calendar、datetime)


在當前月份中循環每一天
大致的思路就是先計算出當月的第一天和下個月的第一天日期,還有當月總共有多少天,然后把第一天日期按照月總天數累加到下個月的第一天,就ok 啦
from datetime import datetime, date, timedelta
import calendar


def get_month_range(start_date=None):
    if start_date is None:
        print(date.today())  # 2019-03-05  # 今天的日期
        start_date = date.today().replace(day=1)  # 修改當前時間。比如修改成當月1號
        print(start_date)  # 2019-03-01  當月的第一天日期
    _, days_in_month = calendar.monthrange(start_date.year, start_date.month)
    print(days_in_month)  # 31   當月總天數31天
    end_date = start_date + timedelta(days=days_in_month)
    print(end_date)  # 2019-04-01
    return (start_date, end_date)


start_date,end_date = get_month_range()
print(start_date,end_date)  #日期范圍 2019-03-01 2019-04-01
a_day=timedelta(days=1)
print(a_day)  # 1 day, 0:00:00
li=[]
while start_date<end_date:
    li.append(start_date)
    start_date+=a_day
# print(li)
# [datetime.date(2019, 3, 1), datetime.date(2019, 3, 2), datetime.date(2019, 3, 3), datetime.date(2019, 3, 4),
# datetime.date(2019, 3, 5), datetime.date(2019, 3, 6), datetime.date(2019, 3, 7), datetime.date(2019, 3, 8),
# .......
# datetime.date(2019, 3, 29), datetime.date(2019, 3, 30), datetime.date(2019, 3, 31)]
# 結束日期並不包含在這個日期范圍內(事實上它是下個月的開始日期)。 這個和Python的 slice 與 range 操作行為保持一致,同樣也不包含結尾。


# 使用生成器優化這段代碼
def date_range(start,stop,step):
    if start<stop:
        yield start
        li.append(start)
        start+=step
date_range(start_date,end_date,timedelta(days=1))

# print(li)

補充:datetime

now = datetime.now()
print(now)
# 返回一個time結構
print(
    now.timetuple())  # time.struct_time(tm_year=2019, tm_mon=3, tm_mday=5, tm_hour=9, tm_min=41, tm_sec=52, tm_wday=1, tm_yday=64, tm_isdst=-1)
# 返回一個date類型
print(now.date())  # 2019-03-05
# 返回一個time類型
print(now.time())  # 09:41:52.715695
# 當前星期幾。星期一是0,星期於是6   注意這里是方法,不是屬性哦。
print(now.weekday())  # 1
# 當前星期幾。星期一是1,星期日是7注意這里是方法,不是屬性哦。
print(now.isoweekday())  # 2
# 修改當前時間。比如修改成當月1號
print(now.replace(day=1))  # 2019-03-01 09:41:52.715695
past = datetime(2010, 11, 12, 13, 14, 15, 16)  # 2010-11-12 13:14:15.000016
print(past)
# 進行比較運算返回的是timedelta類型
print(now - past)  # 3034 days, 20:27:37.715679
# 轉成字符串
strdatetime = now.strftime("%Y-%m-%d %H:%M:%S")
print(strdatetime)  # 2019-03-05 09:41:52
# 字符串生成datetime對象
print(datetime.strptime(strdatetime, "%Y-%m-%d %H:%M:%S"))  # 2019-03-05 09:41:52
返回指定年的某月:calendar
import calendar


def get_month(year, month):
    return calendar.month(year, month)


# 返回指定年的日歷
def get_calendar(year):
    return calendar.calendar(year)


# 判斷某一年是否為閏年,如果是,返回True,如果不是,則返回False
def is_leap(year):
    return calendar.isleap(year)


# 返回某個月的weekday的第一天和這個月的所有天數
def get_month_range(year, month):
    return calendar.monthrange(year, month)


# 返回某個月以每一周為元素的序列
def get_month_calendar(year, month):
    return calendar.monthcalendar(year, month)


# 返回指定年的日歷
def get_calendar(year):
    return calendar.calendar(year)


# 判斷某一年是否為閏年,如果是,返回True,如果不是,則返回False
def is_leap(year):
    return calendar.isleap(year)


# 返回某個月的weekday的第一天和這個月的所有天數
def get_month_range(year, month):
    return calendar.monthrange(year, month)  # (3, 31)


# 返回某個月以每一周為元素的序列
def get_month_calendar(year, month):
    return calendar.monthcalendar(year, month)


year = 2013
month = 8
test_month = get_month(year, month)
print(test_month)
print('#' * 50)
# print(get_calendar(year))
print('{0}這一年是否為閏年?:{1}'.format(year, is_leap(year)))
print(get_month_range(year, month))
print(get_month_calendar(year, month))
"""
"""
 August 2013
Mo Tu We Th Fr Sa Su
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

##################################################
2013這一年是否為閏年?:False
(3, 31)
[[0, 0, 0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24, 25], [26, 27, 28, 29, 30, 31, 0]]
"""

 


免責聲明!

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



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