0

I want to be able to get two time stamps and compare if the second (later taken) one is less than 59 minutes away from the first one. Following this thread Compare two dates with JavaScript the date object may do the job.

But first thing I am not happy with is that it takes the time from my system. Is it possible to get the time from some public server or something? Cause there always is a chance that the system clock gets manipulated within the time stamps, so that would be too unreliable. Some outside source would be great.

Then I am not too sure how to get the difference between 2 times (using 2 date objects). I The times could be something like 3:59 and 6:12, so just comparing minutes would give the wrong idea. Thus we should consider hours too. But then there the issue with the modulo 24. Day 3 23:59 and day 4 0:33 wouldn't produce proper results either.

So including days too. then the modulo 30 thing, even though that on top changes month for month. So month and year need to be included as well.

Hence, we would need the whole date, everything from current year to second (because second would be nice too, for precision) and comparing them would require tons of if clauses for year, month, etc.

Do the date objects have some predefined date comparison function that actually keeps all these things in mind (haven't even mentioned leap years yet, have I)?

The time would be very important cause exactly at the 59 minutes mark (+-max 5 seconds wouldn't matter but getting close to 60 is forbidden) a certain function would have to be used that without fail closes a website. The script opens website at mark 0 min, does some stuff, rinse and repeat style, and closes the page at the 59 min mark.

Checking the time like every few seconds would be smart.

Any good ideas how to implement such a time comparison that doesn't take too more computer power yet is efficient as in new month starting and stuff doesn't mess it up?

tripleee
  • 175,061
  • 34
  • 275
  • 318
bernd
  • 1
  • 1

1 Answers1

0

You can compare the two Date times, but when creating a date time there is a parameter of DateTime(value) which you can use.

You can use this API to get the current UTC time which returns a example JSON array like this:

{ 
   "$id":"1",
   "currentDateTime":"2019-11-09T21:12Z",
   "utcOffset":"00:00:00",
   "isDayLightSavingsTime":false,
   "dayOfTheWeek":"Saturday",
   "timeZoneName":"UTC",
   "currentFileTime":132178075626292927,
   "ordinalDate":"2019-313",
   "serviceResponse":null
}

So you can use either the currentFileTime or the currentDateTime return from that API to construct your date object.

Example:

const date1 = new Date('2019-11-09T21:12Z') // time when I started writing this answer
const date2 = new Date('2019-11-09T21:16Z') // time when I finished writing this answer

const diff = new Date(date2-date1)
console.log(diff.toTimeString()) // time it took me to write this

Please keep in mind that due to network speeds, the time API will be a little bit off (by a few milliseconds)

OverHash
  • 3
  • 1
  • 4
  • is currentfiletime something like the seconds that passed since a certain fixed time point? like the seconds that passed since the 1/1/2000 0:00 ? that would be by far the easiest thing I could use to calculate a time difference! :o – bernd Nov 10 '19 at 14:58