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 
2021-06-19 12:34:34
96 days, 0:00:00
21-03-15 
2015-03-21 00:00:00 
2021-03-15 00:00:00 
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] 
1999-04-23 00:00:00 
1999-04-24 00:00:00 

"""

你可能感兴趣的:(python,开发语言,pandas)