如何比较javascript中的日期对象以检查对象是否至少5分钟?我试过了:
//current date
var date = new Date();
//data from database
console.log("db: " + data_from_database)
console.log("current date: " + date)
//how to check if "data_from_database is at least 5 minutes old?
db:Sun Jul 16 2017 23:59:59 GMT + 0200(CEST)
当前日期:2017年7月16日星期日 格林威治标准时间14:12:43 + 0200(CEST)
如何在不创建大量新对象的情况下以最简单的方式执行检查?
答案 0 :(得分:3)
当您subtract
两个日期时,您会得到以毫秒为单位的时间,然后您可以compare
var date = new Date();
//data from database
console.log("db: " + data_from_database)
console.log("current date: " + date)
var FIVE_MIN=5*60*1000;
if((date - new Date(date_from_database)) > FIVE_MIN) {
console.log('Delayed by more than 5 mins');
}
答案 1 :(得分:0)
guard
let dict = response as? [String : Any],
let hits = dict["hits"] as? [String : Any],
let hitArray = hits["hits"] as? [[String : Any]]
else
{ throw Abort}
for hit in hitArray {
if let source = hit["_source"] {
arrayOfItems.append(Item(with: source))
}
}