2017-07-24 44 views
-2

让我说我有一个字段resolved.time。 resolved.time =“17年7月6日十四时19分39秒”如何获取条件哪里日期> 24小时

我想写JavaScript的一个条件是这样的:

if resolved.time>24 hours{ 
print("true"); 
} 
else 
{ 
print("false"); 
} 

我怎么能写出这种情况下,“如果解决。时间> 24小时“在JS? 感谢您的帮助。

感谢和问候, Okik

+0

在时间(日期)上的点怎么能在一段时间 “大于”?这就像说“明天会比八十年代少吗?”或更糟糕“是比所有奶酪更长的洋葱?” –

+0

是的,我的意思是,大于一段时间的解决时间,意味着明天。 – user3418286

回答

-1

let resolved = {} 
 
resolved.time = "07/06/17 14:19:39" 
 

 
// convert resolved.time to milliseconds 
 
const time = (new Date(resolved.time)).getTime() 
 

 
const dayInMs = 24 * 60 * 60 * 1000 
 

 

 
// check if duration between now and `time is greater than 24h 
 
if (Date.now() - time > dayInMs) { 
 
    console.log("true"); 
 
} else { 
 
    console.log("false"); 
 
}

+0

请不要推荐使用内置解析器解析字符串,请参阅[*为什么Date.parse提供不正确的结果?](https://stackoverflow.com/questions/2587345/why-does-date-parse-give -incorrect-results) – RobG

+0

@RobG我不知道'Date.parse()'。谢谢你的提示。 – marzelin

相关问题