python 日期计算各种方式

得意时要看淡,失意时要看开。不论得意失意,切莫大意;不论成功失败,切莫止步。志得意满时,需要的是淡然,给自己留一条退路;失意落魄时,需要的是泰然,给自己觅一条出路python 日期计算各种方式,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

    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'>

"""

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/156902.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!