所以我试图使用datetime创建一个简单的代码并遇到错误。
$quantity_per_product = array();
//Loop trough the order items and see if it is a product we need to cehck
foreach($items as $item) {
if($subtotal== 0 && in_array($item['product_id'], $target_products)) {
$_product_id = $item['product_id'];
if(isset ($_product_id)){
if(array_key_exists($_product_id, $quantity_per_product))
{
$quantity_per_product[$_product_id] += $item['qty']
}
else{
$quantity_per_product[$_product_id] = $item['qty']
}
}
}
}
foreach( $quantity_per_product as $p_id => $qty){
echo '('. get_the_title($p_id). ')# You bought '.$qty.' Times<br>';
}
结果我得到了
import time
from datetime import datetime
x = True
b = datetime.strptime("06:10", "%H:%M")
while x == True:
a = datetime.now().time()
print(a)
if a > b:
x = False
time.sleep(0.945)
所以我想知道是否可以以datetime.time()格式保存datetime.strptime。
提前致谢
答案 0 :(得分:0)
比较.time()
和a
的{{1}}为:
b
而不是if a > b.time(): # if you want to compare only time
个对象。答案结束时的原因。
datetime.now().time()
属于datetime
类型:
datetime.time
然而,datetime.strptime()
和datetime.now()
属于>>> type(datetime.now().time())
<type 'datetime.time'>
类型:
datetime.datetime
根据 Martijn 的评论进行修改。
创建>>> type(datetime.strptime("06:10", "%H:%M"))
<type 'datetime.datetime'>
>>> type(datetime.now())
<type 'datetime.datetime'>
对象时,datetime
日期将设置为datetime.strptime("06:10", "%H:%M")
。绝对我不认为你想与之比较。您可以将日期检查为:
1900-01-01
答案 1 :(得分:0)
您尝试比较datetime
和time
对象,Python不允许您进行比较。
如果你datetime.strptime()
,你会得到一个包含日期和时间的对象(称为datetime
)。但是因为您还没有解析默认为01-01-1900的日期。现在datetime.now()
也会为您提供datetime
对象,但具有当前日期。因此,直接比较datetime.now()
和b
是否有效,因为日期不同。
现在您只使用datetime.now().time()
来使用当前时间,因此您还需要在比较前的某个位置b
将其应用于b = b.time()
。
答案 2 :(得分:0)
使用'datetime.now()'insted of'datetime.now()。time()'