Python 日期处理——datetime包学习(下)

加载包

# -*- coding: utf-8 -*-
from datetime import date, timedelta
# 主要用到timedelta()方法和date.replace()方法

一 、基础函数

current_date = date(2015, 8, 22)
date1 = current_date
date2 = date(2015, 8, 10)
dd = current_date


# 当前日期所在的星期的第一天
def first_day_of_current_week(current_date):
    return current_date - timedelta(days=current_date.weekday())


# 当前日期所在的月份的第一天
def first_day_of_current_month(current_date):
    return current_date.replace(day=1)


# 两个日期之间的日期字符串列表(左闭右开) 
def days_between_two_dates(date1, date2):
    n_days = abs((date1 - date2).days)
    end = date2 if date2 > date1 else date1
    if n_days == 1:
        return  str(end - timedelta(days=1))        
    return [str(end - timedelta(days=x)) for x in range(n_days, 0, -1)]
>>> first_day_of_current_week(current_date)
datetime.date(2015, 8, 17)

>>> first_day_of_current_month(current_date)
datetime.date(2015, 8, 1)

>>> days_between_two_dates(date1, date2)
['2015-08-10', '2015-08-11', '2015-08-12', '2015-08-13', '2015-08-14', '2015-08-15', '2015-08-16', '2015-08-17', '2015-08-18', '2015-08-19', '2015-08-20', '2015-08-21']

二、 核心函数

  • 星期处理
# 获取当前日期所在的星期的日期字符串列表
def days_of_this_week(current_date, just2dd=True):
    first_day_of_this_week = first_day_of_current_week(current_date)
    first_day_of_next_week = first_day_of_this_week + timedelta(days=7)
    if just2dd:
        return days_between_two_dates(first_day_of_this_week, current_date)       
    return days_between_two_dates(first_day_of_this_week,
                                  first_day_of_next_week)



# 获取当前日期上一星期的日期字符串列表
def days_of_last_week(current_date):
    first_day_of_this_week = first_day_of_current_week(current_date)
    first_day_of_next_week = first_day_of_this_week - timedelta(days=7)
    return days_of_this_week(first_day_of_next_week, just2dd=False)


>>> days_of_this_week(current_date, just2dd=True)
['2015-08-17', '2015-08-18', '2015-08-19', '2015-08-20', '2015-08-21']

>>> days_of_last_week(current_date)
['2015-08-10', '2015-08-11', '2015-08-12', '2015-08-13', '2015-08-14', '2015-08-15', '2015-08-16']

  • 月份处理
# 获取当前日期所在的月份的日期字符串列表,默认截至到当前日期
def days_of_this_month(dd=date.today(), just2dd=True):
    first_day_of_this_month = first_day_of_current_month(dd)
    if dd.month == 12:
      first_day_of_next_month = dd.replace(year=dd.year+1, month=1, day=1)   
    else:
      first_day_of_next_month = dd.replace(month=dd.month+1, day=1)
    if just2dd:
        return days_between_two_dates(dd, first_day_of_this_month)      
    return days_between_two_dates(first_day_of_this_month, 
                                  first_day_of_next_month)


# 获取当前日期前一月份的日期字符串列表
def days_of_last_month(dd=date.today()):
    last_day_of_last_month = first_day_of_current_month(dd) - timedelta(days=1)
    return days_of_this_month(last_day_of_last_month, just2dd=False)
>>> days_of_this_month(dd=date.today(), just2dd=True)
['2015-08-01', '2015-08-02', '2015-08-03', '2015-08-04', '2015-08-05', '2015-08-06', '2015-08-07', '2015-08-08', '2015-08-09', '2015-08-10', '2015-08-11', '2015-08-12', '2015-08-13', '2015-08-14', '2015-08-15', '2015-08-16', '2015-08-17', '2015-08-18', '2015-08-19', '2015-08-20', '2015-08-21']

>>> days_of_last_month(dd=date.today())
['2015-07-01', '2015-07-02', '2015-07-03', '2015-07-04', '2015-07-05', '2015-07-06', '2015-07-07', '2015-07-08', '2015-07-09', '2015-07-10', '2015-07-11', '2015-07-12', '2015-07-13', '2015-07-14', '2015-07-15', '2015-07-16', '2015-07-17', '2015-07-18', '2015-07-19', '2015-07-20', '2015-07-21', '2015-07-22', '2015-07-23', '2015-07-24', '2015-07-25', '2015-07-26', '2015-07-27', '2015-07-28', '2015-07-29', '2015-07-30', '2015-07-31']

  • 获取当前日期前n天的日期字符串列表(左闭右开,即不包含当前日期)
def last_n_days(current_date, n=0):
    if n in (0,1):
        return str(current_date - timedelta(days=n))
    return [str(current_date - timedelta(x)) for x in range(n, 0, -1)]
>>> last_n_days(current_date, n=1)
'2015-08-21'

>>> last_n_days(current_date, n=4)
['2015-08-18', '2015-08-19', '2015-08-20', '2015-08-21']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值