在python中无法将更改后的日期时间转换为时间戳

2024-05-18 23:30:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把timestamps转换成dates,并截断minuteseconds,然后,将其转换回timestamps。我知道我可以用一些数学算法,比如timestamps/60timestamps/3600,但是我想使用Python提供的函数。我的代码如下。我尝试了两种方法:

for timeStamp in lineTimeStamps:
    #first try 
    day1=datetime.datetime.\
        fromtimestamp(float(timeStamp)).strftime("%Y-%m-%d %H")
    timestamp1 = time.mktime(day1)
    #second try 
    day2=datetime.datetime.\
        fromtimestamp(float(timeStamp)).\
        replace( minute=0, second=0, microsecond=0).timetuple()
    timestamp2 = time.mktime(day2)

timestamp1 = time.mktime(day1)中,我得到了如下错误:

TypeError: Tuple or struct_time argument required

timestamp2 = time.mktime(day2)中,我得到的错误如下:

OverflowError: mktime argument out of range

如何将其转换回timestamps


Tags: datetimetime错误floattimestampsecondtrytimestamps

热门问题