0

my issue is that 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). many issue that may pop up: time being something like 3:59 and 6:12 so just comparing minutes would give the wrong idea. so we consider hours too. biut there the issue with the modulo 24. day 3 23:59 and day 4 0:33 wouldnt be viewed proper either.

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

so 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 predfeined date comparision function that actually keeps all these things in mind (havent even mentioned leap years yet, have I)?

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

checking the time like every few seconds would be smart.

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

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
  • 28
  • 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