设为首页收藏本站
查看: 68|回复: 0

[PHP] python——计算上周5的日期

[复制链接]

论坛元老

Rank: 6Rank: 6

积分
34274
主题
17031
UID
1347
M币
67
贡献
17176

  • 发表于 2017-5-14 02:44:00 | 显示全部楼层 |阅读模式
    第一种方法:
    Python代码
    from datetime import datetime, timedelta
    weekdays = ['Monday','Tuesday','Wednesday','Thursday',
    'Friday','Saturday','Sunday']
    def get_previous_byday(dayname, start_date=None):
    if start_date is None:
    start_date = datetime.today()
    day_num = start_date.weekday()
    day_num_target = weekdays.index(dayname)
    days_ago = (7 + day_num - day_num_target) % 7
    if days_ago == 0:
    days_ago = 7
    target_date = start_date - timedelta(days = days_ago)
    return target_date

    print( datetime.today() )
    print( get_previous_byday('Monday') )
    print( get_previous_byday('Monday', datetime(2016, 8, 28)) )
    第二种方法,用dateutil模块
    Python代码
    from datetime import datetime
    from dateutil.relativedelta import relativedelta
    from dateutil.rrule import *
    d = datetime.now()
    print(d)
    print(d + relativedelta(weekday=FR))
    print(d + relativedelta(weekday=FR(-1)))
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    在我站开通SVIP可同时获得17个站点VIP资源 立即登录 立即注册
    快速回复 返回顶部 返回列表