zoukankan      html  css  js  c++  java
  • python关键日期计算

    在coding的过程中有时候会需要用到一些特殊日期,比如说是一个月的最后一天的日期,昨天的日期等等。

     1     def first_day_of_next_month(self, old_date):
     2         old_date = datetime.datetime.strptime(old_date, "%Y-%m-%d")
     3         next_month = old_date.replace(day=28) + datetime.timedelta(days=4)
     4         next_month = next_month.replace(day=1)
     5         return next_month.strftime("%Y-%m-%d")
     6     
     7     def last_day_of_month(self, any_day):
     8         any_day = datetime.datetime.strptime(any_day, "%Y-%m-%d")
     9         next_month = any_day.replace(day=28) + datetime.timedelta(days=4)  # this will never fail
    10         return (next_month - datetime.timedelta(days=next_month.day)).strftime("%Y-%m-%d")
    11 
    12     def get_last_day(self, current_date):
    13         current_date = datetime.datetime.strptime(current_date, "%Y-%m-%d")
    14         last_date = current_date + datetime.timedelta(days=1)
    15         return last_date.strftime("%Y-%m-%d")
  • 相关阅读:
    网络流入门
    Sereja and Swaps(贪心+暴力枚举区间)
    multiset
    欧拉路
    整除分块
    蓝魔法师
    选点
    F. Tree with Maximum Cost(换根)
    “db2执行sql语句,注释没了”的解决办法
    Mybatis中的自带Mapper方法
  • 原文地址:https://www.cnblogs.com/dream2sky/p/12312122.html
Copyright © 2011-2022 走看看