首页 > 解决方案 > 如何解析时间戳值并更改时间戳值

问题描述

给定这个时间戳值2019-01-29T16:22:54+00:00(格式为 YYYY-MM-DDThh:mm:ss±hh:mm)

我需要将最后的 00:00(对应于 hh:mm)更改为“Z”

标签: pysparktimestampunix-timestamp

解决方案


检查下面的简单示例,您可以根据您的更多要求进行调整

def convert_z_time(time_string):
    if time_string[-5:] == '00:00':
        converted_time = '{}Z'.format(time_string[:-6])
    else:
        converted_time = time_string
    return converted_time


time_string = '2019-01-29T16:22:54+00:00'    
print(convert_z_time(time_string))

time_string = '2019-01-29T16:22:54+10:00'    
print(convert_z_time(time_string))

输出应该是

2019-01-29T16:22:54Z
2019-01-29T16:22:54+10:00

希望这对你有用


推荐阅读