当秒是浮点值时,如何在python 3x中创建datetime对象

当秒是浮点值时,如何在python 3x中创建datetime对象,python,datetime,Python,Datetime,我需要创建一个带有年、月、日、小时、分钟、秒和百分之一秒的datetime对象,保留一秒钟的部分信息 这能做到吗 这失败了: dto = dt.datetime(VLeaderData['Year'], month=VLeaderData['Month'], day=VLeaderData['Day'],hour=VLeaderData['Hour'], minute=VLeaderData['Minute'], second=(VLeaderData['Second']

我需要创建一个带有年、月、日、小时、分钟、秒和百分之一秒的datetime对象,保留一秒钟的部分信息

这能做到吗

这失败了:

dto = dt.datetime(VLeaderData['Year'], month=VLeaderData['Month'],
    day=VLeaderData['Day'],hour=VLeaderData['Hour'],
    minute=VLeaderData['Minute'],
    second=(VLeaderData['Second']+VLeaderData['Hundredths']/100))
因为second必须是整数: TypeError:应为整数参数,得到浮点值


我是否超过了datetime在这里的功能?

不,您没有。您可以将额外信息传递到对象的
微秒
参数:


不,你没有。您可以将额外信息传递到对象的
微秒
参数:


您只需要将所有内容指定为整数。。。如果有秒作为浮点:

seconds = 30.1234
然后取整数部分的秒数,计算微秒数:

secs = int(seconds)
microseconds = int((seconds - secs) * 100000)
或者,(如果我正确理解了您的代码),您有百分之一秒的时间作为字段本身:

microseconds = VLeaderData['Hundredths'] * 1000
然后将其全部传递给
datetime

datetime.datetime(year, month, day, hour, minute, secs, microseconds)

您只需要将所有内容指定为整数。。。如果有秒作为浮点:

seconds = 30.1234
然后取整数部分的秒数,计算微秒数:

secs = int(seconds)
microseconds = int((seconds - secs) * 100000)
或者,(如果我正确理解了您的代码),您有百分之一秒的时间作为字段本身:

microseconds = VLeaderData['Hundredths'] * 1000
然后将其全部传递给
datetime

datetime.datetime(year, month, day, hour, minute, secs, microseconds)

多谢各位,我忘记了第一百次辩论多谢各位,我忘记了第一百次辩论