在我的角度应用程序中,我需要检查当前时间是否大于3PM。我正在使用Date.now()来获取当前时间,并且在构造函数中已经拥有了,所以我会获取每分钟的更新时间,
setInterval(() => {
this.today = Date.now();
}, 1);
现在我需要检查当前时间是否大于3 PM。
答案 0 :(得分:5)
我使用getHours()来获取一天中的时间。它将返回0到23之间的范围;因此,15为您正在寻找的下午3点。
1000*60
也是1分钟的时间(以毫秒为单位)。
setInterval(() => {
this.today = new Date();
if (this.today.getHours() >= 15){
console.log('Current time is greater than 3 PM!');
}
}, 1000*60);
答案 1 :(得分:1)
我将使用setTimeout
方法来设置警报,而不是进行间隔测试。
当我们知道何时完成计算时,就不会浪费时间间隔计算。
// ES6 CLASS
class Alert {
constructor(timestamp) {
this.timestamp = timestamp;
this._passed = false;
this.callbacks = [];
// Test as soon as possible
setTimeout(this.test.bind(this), 0);
}
get passed() {
return this._passed;
}
test() {
if (this.timestamp.getTime() <= Date.now()) {
//Test again after time difference mark as passed
this._passed = true;
//Fire all callbacks
this.callbacks.forEach(cb => cb());
} else {
//Test again after time difference
setTimeout(this.test.bind(this), this.timestamp.getTime() - Date.now());
}
return this;
}
then(callback) {
if (this._passed) {
callback();
} else {
this.callbacks.push(callback);
}
return this;
}
}
//TEST
// Fire in 10 seconds
new Alert(new Date(Date.now() + 5 * 1000))
.then(a => console.log("5 seconds passed"))
.then(a => console.log("The 5 seconds were great!"));
// Fire in 4 seconds
new Alert(new Date(Date.now() + 2 * 1000))
.then(a => console.log("2 seconds passed"))
.then(a => console.log("The 2 seconds were meh!"));
var threeOClock = new Date();
threeOClock.setHours(15, 0, 0);
new Alert(threeOClock)
.then(a => console.log("It's 15 or more!"));
p {
margin: 200px 0px;
}
<pre>// TYPESCRIPT CLASS
class Alert {
constructor(timestamp) {
this.timestamp = timestamp;
this._passed = false;
this.callbacks = [];
// Test as soon as possible
setTimeout(this.test.bind(this), 0);
}
get passed() {
return this._passed;
}
test() {
if (this.timestamp.getTime() <= Date.now()) {
//Test again after time difference mark as passed
this._passed = true;
//Fire all callbacks
this.callbacks.forEach(cb => cb());
}
else {
//Test again after time difference
setTimeout(this.test.bind(this), this.timestamp.getTime() - Date.now());
}
return this;
}
then(callback) {
if (this._passed) {
callback();
}
else {
this.callbacks.push(callback);
}
return this;
}
}
//TEST
// Fire in 10 seconds
new Alert(new Date(Date.now() + 5 * 1000))
.then(a => console.log("5 seconds passed"))
.then(a => console.log("The 5 seconds were great!"));
// Fire in 4 seconds
var fourSecondAlert = new Alert(new Date(Date.now() + 2 * 1000))
.then(a => console.log("2 seconds passed"))
.then(a => console.log("The 2 seconds were meh!"));
</pre>
编辑1-承诺
如果您对诺言感到满意并且只需要一个简单的警报,则应该这样做:
function Alert(date) {
return new Promise(function(res) {
if (Date.now() >= date.getTime()) {
res();
} else {
setTimeout(res, date.getTime() - Date.now());
}
});
}
//TEST
Alert(new Date(Date.now() + 5 * 1000))
.then(a => console.log("5 seconds passed"))
.then(a => console.log("The 5 seconds were great!"));
Alert(new Date(Date.now() + 2 * 1000))
.then(a => console.log("2 seconds passed"))
.then(a => console.log("The 2 seconds were meh!"));
var threeOClock = new Date();
threeOClock.setHours(15, 0, 0);
Alert(threeOClock)
.then(a => console.log("It's 15 or more!"));
创建日期对象并设置时间
创建时间戳的最简单方法是创建Date
对象并使用setHours对其进行修改:
var threeOClock = new Date();
threeOClock.setHours(15, 0, 0);
console.log(threeOClock.toTimeString());
答案 2 :(得分:0)
要检查当前时间是否大于3 PM,最好的方法是使用力矩库。请看看它是如何完成的here