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);
}
}
The getTime() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.
This number is called a timestamp. eg 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.
eg 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 compare the values as you did already in the example
if(time <= now && now <= nextTime) {
// Highlight the column
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.