The problem is your timezone.
When you do new Date(difference)
, you're creating a Date
object that represent the moment exatcly difference
milliseconds after January 1st, 1970. When you do lapse.getHours()
your timezone is used in the computation. You cannot modify your timezone via Javascript, and cannot modify this behaviour. Not without some heavy Javascript tricks.
But your difference
does not represent a date, but a difference of dates. Treat is as such, and compute the hours, minutes and seconds like this:
var hours = Math.floor(difference / 36e5),
minutes = Math.floor(difference % 36e5 / 60000),
seconds = Math.floor(difference % 60000 / 1000);
Alternatively, you can take your timezone into account when creating lapse
:
var lapse = new Date(difference + new Date().getTimezoneOffset() * 1000);
but I wouldn't recommend this: Date
objects are overkill for your purposes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…