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

Check if time between 2 times in Javascript - Stack Overflow

programmeradmin1浏览0评论

I'm using google sheets javascript script where I'm looping over 15 min intervals each in their own column. I'm trying to find the column that matches the current hour/min so I can highlight it but can't get the javascript time checking to work.

time and nextTime are simply times like 8:00 or 8:15, so when I convert them to Date I get something like: Sat Dec 30 1899 08:00:00 GMT-0600 (CST)

Does getTime() consider the date part or just the time part? If I have a string for the date like "10/14/2017" how could I bring the 2 together for the date object?

var now = new Date().getTime();

  for(i in data[0]){
    var time = new Date(data[0][i]);
    var nextTime = new Date(data[0][Number(i) + 1]);

    var diff1 = time.getTime() - now;
    var diff2 = nextTime.getTime() - now;

    if(now >= time.getTime() && now < nextTime.getTime()){
      Logger.log(time);
    }
  }

I'm using google sheets javascript script where I'm looping over 15 min intervals each in their own column. I'm trying to find the column that matches the current hour/min so I can highlight it but can't get the javascript time checking to work.

time and nextTime are simply times like 8:00 or 8:15, so when I convert them to Date I get something like: Sat Dec 30 1899 08:00:00 GMT-0600 (CST)

Does getTime() consider the date part or just the time part? If I have a string for the date like "10/14/2017" how could I bring the 2 together for the date object?

var now = new Date().getTime();

  for(i in data[0]){
    var time = new Date(data[0][i]);
    var nextTime = new Date(data[0][Number(i) + 1]);

    var diff1 = time.getTime() - now;
    var diff2 = nextTime.getTime() - now;

    if(now >= time.getTime() && now < nextTime.getTime()){
      Logger.log(time);
    }
  }
Share Improve this question asked Oct 14, 2017 at 16:17 user441521user441521 6,99826 gold badges98 silver badges167 bronze badges 4
  • 1 I could probably give a long answer, but the best thing you can do here is use moment.js. Date objects in javascript sux, moment makes it easy to deal with – iagowp Commented Oct 14, 2017 at 16:25
  • look here - stackoverflow./a/16080662/6149665 – ArtemSky Commented Oct 14, 2017 at 16:27
  • 1 Always create Date object by using format var d = new Date(year, month, day, hours, minutes, seconds, milliseconds); if you have less parameters then use var d = new Date(year, month, day, hours, minutes); because other formats may or may not work in all browsers. check link for reference w3schools./jsref/jsref_obj_date.asp – Baba Khedkar Commented Oct 14, 2017 at 16:30
  • OK, I added a string for the current date I was interested in and built it form scratch. Kind of a pain but it works accept for I want to use 12 hour not 24 and it that if statement seems to fail at that. – user441521 Commented Oct 14, 2017 at 17:53
Add a ment  | 

1 Answer 1

Reset to default 5

The getTime() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.

This number is called a timestamp. e.g. 1508001790301. This number represents the total number of milliseconds (so it considers both the date part and the time part)

You can convert the string "10/14/2017" to a timestamp by passing it as a Date constructor argument and calling the getTime method on the newly created date object.

e.g. var nextTime = new Date('10/14/2017').getTime(); This will return the number of milliseconds that have passed since January 1970 till Oct 14 2017 00:00:00.

To bring everything together:

1) Get the current timestamp

var now = new Date().getTime(); // 1508001790301

2) Get the timestamps for time and nextTime

var time = new Date('10/14/2017').getTime(); 
var nextTime = new Date('10/15/2017').getTime();

3) Simply pare the values as you did already in the example

if(time <= now && now <= nextTime) {
      // Highlight the column
}
发布评论

评论列表(0)

  1. 暂无评论