相关疑难解决方法(0)

Python不比较日期?

我有这个launch_time:

2015-01-15 10:31:54 + 00:00

我得到current_time

current_time = datetime.datetime.now(launch_time.tzinfo)
Run Code Online (Sandbox Code Playgroud)

我希望两次都相同所以我使用了tzinfo.所以,current_time的值是

2015-01-16 10:55:50.200571 + 00:00

我用这个运行时间:

running_time = (current_time - launch_time).seconds/60
Run Code Online (Sandbox Code Playgroud)

该值仅返回23分钟.它应该是一天+ 23分钟= 1463分钟

有人能帮我吗.谢谢

python time datetime compare date

1
推荐指数
1
解决办法
91
查看次数

从datetime对象中减去datetime.datetime.now()

我有一个字符串,我已使用解析转换为日期时间对象.

time = 'Tue, 30 Sep 2014 16:19:08 -0700 (PDT)'
date_object = parse(time)
Run Code Online (Sandbox Code Playgroud)

我想找到从那时起已经过去的时间.我尝试使用datetime.datetime.now()和减去这两个,但它们是不同的格式,并抛出一个错误.

做这个的最好方式是什么?

python datetime

1
推荐指数
1
解决办法
624
查看次数

python时间与日期时间

以下比较不起作用,有没有办法从时间模块中获取自纪元以来经过的秒数(wrt localtime)?

(datetime.datetime.now() - datetime.datetime.utcfromtimestamp(0)).total_seconds() - time.mktime(time.localtime()) => 3600.9646549224854
Run Code Online (Sandbox Code Playgroud)

或者

(datetime.datetime.now() - datetime.datetime.utcfromtimestamp(0)).total_seconds() - time.time() => 3599.9999861717224
Run Code Online (Sandbox Code Playgroud)

谢谢

python time datetime

0
推荐指数
1
解决办法
3964
查看次数

从python中的timedelta输出中删除时间

我编写了一个小的 python 脚本来确定记录的年龄以及它是否超过一周。我通过从 中减去记录日期来做到这一点now()。该记录将日期2016-02-23 09:01:22作为日期时间返回。

now = datetime.datetime.now()
age = now - recordDate
print age
Run Code Online (Sandbox Code Playgroud)

这将结果打印为 71 days, 23:56:07.156000

有没有比我的 hack 解决方案更“pythonic”的方法来让它只输出几天(而不是几个小时等)

print str(age).split(',',1)[0]
Run Code Online (Sandbox Code Playgroud)

将结果打印为71 days

python time datetime

0
推荐指数
1
解决办法
3361
查看次数

获取当前日期/时间并与其他日期进行比较

我正在尝试获取当前时间,并将其与从字符串中获取的日期进行比较。这是我的代码:

import datetime

CurrentDate = str(datetime.datetime.now())
CurrentDate = datetime.strptime(CurrentDate, "%d/%m/%Y %H:%M")
print(CurrentDate)

ExpectedDate = "9/8/2015 4:00"
ExpectedDate = datetime.datetime.strptime(ExpectedDate, "%d/%m/%Y %H:%M")
print(ExpectedDate)

if CurrentDate > ExpectedDate:
    print("Date missed")
else:
    print("Date not missed")
Run Code Online (Sandbox Code Playgroud)

但这是我得到的错误。

CurrentDate = datetime.strptime(CurrentDate,“%d /%m /%Y%H:%M”)AttributeError:'模块'对象没有属性'strptime'

python python-3.x

-1
推荐指数
1
解决办法
8721
查看次数

标签 统计

python ×5

datetime ×4

time ×3

compare ×1

date ×1

python-3.x ×1