二十、时间系列之年月日时

返回2000-01-01 18 这种格式

import datetime
import time

class Time:

    def hour(self, add_hour,now_time=False):
        """
        返回年月日时
        :param add_hour: 小时可加可减
        :return:
        """
        if now_time:
            year_month_day_hour = now_time
        else:
            year_month_day_hour = time.strftime('%Y-%m-%d %H', time.localtime(time.time()))

        hour = year_month_day_hour[11:13]
        hour_total = add_hour + int(hour)
        multiple = hour_total // 24

        # 要返回的时间属于过去到至前一天
        if hour_total < -24:
            year_month_day = self.__day(multiple)
            hour = self.__format_processing(abs(hour_total % 24))

            return year_month_day + hour

        # 要返回的时间属于过去的一天
        elif -24 <= hour_total <= 0:
            if hour_total == 0:
                year_month_day = self.__day(0)
                hour = ' 00'
            else:
                year_month_day = self.__day(-1)
                hour = self.__format_processing(str(hour_total + 24))

            return year_month_day + hour

        # 要返回的时间属于当天
        elif 0 < hour_total <= 24:
            if hour_total == 24:
                return self.__day(1) + ' 00'
            return year_month_day_hour[0:10] + self.__format_processing(hour_total)

        # 要返回的时间属于明天至未来
        elif 24 < hour_total:
            year_month_day = self.__day(multiple)
            hour = self.__format_processing(hour_total % 24)

            return year_month_day + hour

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bug来袭~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值