最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular time comparison in Typescript - Stack Overflow

programmeradmin2浏览0评论

In my angular application, I need to check if the current time is greater than 3PM. I am getting current time using Date.now() and in constructor I have, so I am getting updated time for every minute,

 setInterval(() => {
            this.today = Date.now();           
          }, 1);

Now I need to check if the current time is greater than 3 PM.

In my angular application, I need to check if the current time is greater than 3PM. I am getting current time using Date.now() and in constructor I have, so I am getting updated time for every minute,

 setInterval(() => {
            this.today = Date.now();           
          }, 1);

Now I need to check if the current time is greater than 3 PM.

Share Improve this question asked Jan 8, 2019 at 10:40 Madasu KMadasu K 1,8632 gold badges39 silver badges77 bronze badges 2
  • FYI, setInterval(fn, milliseconds), so you are checking every millisecond not every minute – Ashish Ranjan Commented Jan 8, 2019 at 10:42
  • You want to pare hours. Create a Date object const dateNow = new Date(Date.now()) et check if it's greater than 3 pm if (dateNow.getHours() > 15)... – Florian Commented Jan 8, 2019 at 10:45
Add a ment  | 

3 Answers 3

Reset to default 5

I use getHours() to get the hours of the day. It will return in a range between 0 to 23; therefore, 15 is the 3 PM that you are looking for.

Also 1000*60 is the number of time for 1 minute in milliseconds.

setInterval(() => {
  this.today = new Date();
  if (this.today.getHours() >= 15){
    console.log('Current time is greater than 3 PM!');
  }
}, 1000*60);

Instead of testing in an interval i would use the setTimeout method to set an alarm.

No sense wasting putations on an interval, when we know when it will be done.

// 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() &lt;= Date.now()) {
            //Test again after time difference mark as passed
            this._passed = true;
            //Fire all callbacks
            this.callbacks.forEach(cb =&gt; 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 =&gt; console.log("5 seconds passed"))
    .then(a =&gt; console.log("The 5 seconds were great!"));
// Fire in 4 seconds
var fourSecondAlert = new Alert(new Date(Date.now() + 2 * 1000))
    .then(a =&gt; console.log("2 seconds passed"))
    .then(a =&gt; console.log("The 2 seconds were meh!"));
</pre>

EDIT 1 - Promises

If you are fortable with promises and just need a simple alarm, then this should do it:

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!"));

Create Date object and set time

The easiest way to create a timestamp is to create Date object and modify it using setHours:

var threeOClock = new Date();
threeOClock.setHours(15, 0, 0);
console.log(threeOClock.toTimeString());

To check if the current time is greater than 3 PM the best will be to use moment library. Please take a look how it was done here

发布评论

评论列表(0)

  1. 暂无评论