python 日期计算各种方式

    def process_date(self):
        start = datetime.datetime(2021,3,15,12,34,34)
        print(start,type(start))
        # timedelta提供参数
        #             days: float = ...,
        #             seconds: float = ...,
        #             microseconds: float = ...,
        #             milliseconds: float = ...,
        #             minutes: float = ...,
        #             hours: float = ...,
        #             weeks: float = ...,
        end = start + datetime.timedelta(weeks=12,days=12)
        print(end)
        # 直接减就能获取时间差
        print(end-start)

        # datetime 转 str
        str_start = start.strftime("%y-%m-%d")
        print(str_start,type(str_start))

        # str 转 datetime
        datetime_parser = parse(str_start)
        print(datetime_parser,type(datetime_parser))

        # str 转 datetime
        datetime_start = datetime.datetime.strptime(str_start,"%y-%m-%d")
        print(datetime_start,type(datetime_start))

        # str 转 datetime
        str_obj = self.get_df()["datetime"]
        # print(str_obj)

        # str 转 datetime
        pd_datetime = pd.to_datetime(str_obj)
        print(pd_datetime,type(pd_datetime))

        rea = pd.date_range(start="1999.04.23 12:22:22",periods=2 ,normalize=True)
        for i in rea:
            datetime_parser_ = parse(str(i))
            print(datetime_parser_,type(datetime_parser_))


"""
2021-03-15 12:34:34 <class 'datetime.datetime'>
2021-06-19 12:34:34
96 days, 0:00:00
21-03-15 <class 'str'>
2015-03-21 00:00:00 <class 'datetime.datetime'>
2021-03-15 00:00:00 <class 'datetime.datetime'>
0        2021-01-04
1        2021-01-04
2        2021-01-04
3        2021-01-04
4        2021-01-04
            ...    
125233   2021-10-12
125234   2021-10-12
125235   2021-10-12
125236   2021-10-12
125237   2021-10-12
Name: datetime, Length: 125238, dtype: datetime64[ns] <class 'pandas.core.series.Series'>
1999-04-23 00:00:00 <class 'datetime.datetime'>
1999-04-24 00:00:00 <class 'datetime.datetime'>

"""

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值