What's the most elegant way to get the end of the day (datetime)?

前端 未结 6 1538
无人及你
无人及你 2021-02-14 10:16

I\'m currently writing some reporting code that allows users to optionally specify a date range. The way it works (simplified), is:

  • A user (optionally) specifies a
6条回答
  •  被撕碎了的回忆
    2021-02-14 10:57

    from datetime import datetime, date, timedelta
    
    def get_current_timestamp():
        return int(datetime.now().timestamp())
    
    def get_end_today_timestamp():
        # get 23:59:59
        result = datetime.combine(date.today() + timedelta(days=1), datetime.min.time())
        return int(result.timestamp()) - 1
    
    def get_datetime_from_timestamp(timestamp):
        return datetime.fromtimestamp(timestamp)
    
    end_today = get_datetime_from_timestamp(get_end_today_timestamp())
    

提交回复
热议问题