I have made a small code snippet. To tell you how old you are, to the second.
But I have found something strange. I know that time and timezones are a nightmare to work with, but this is within my own country and timezone, so I am a little confused.
If I enter the date. 27-12-2013(dd-MM-yyyy), it will show:
Which is correct. as the time here is: 08:42. The getTimezoneOffset() is -60
But if I go back a day it will show this:
It added an hour!? This is the only date it happens at, and I'm a little confused, because daylight savings time is only applied in spring and fall. The getTimezoneOffset() is ALSO -60
Hope you can shed some light on this.
Timezone: http://www.timezoneconverter.com/cgi-bin/zoneinfo?s=standard&tz=Europe/Copenhagen
Javascript: GMT+0100 (Romance Standard Time)
Country: Denmark
JSFiddle: http://jsfiddle.net/d6kBJ/4/
var $output = $(".js-ageoutput");
var bd = new Date(2000, 0, 01);
bd.setHours(0);
bd.setMinutes(0);
bd.setSeconds(0);
function showTotalAge() {
var ageDifMs = Date.now() - bd.getTime();
var ageDate = new Date(ageDifMs); // miliseconds from epoch
displayAge(ageDate)
loopAge();
}
function displayAge(age) {
var yearOld = Math.abs(age.getFullYear() - 1970);
var months = age.getMonth();
var days = age.getDate() - 1;
var h = age.getHours() - 1;
var m = age.getMinutes();
var s = age.getSeconds();
$output.html(yearOld + ' years <br/>' + months + ' months <br/>' + days + ' days <br/>' + h + 'hours <br/>' + m + ' minutes <br/>' + s + ' seconds<br/>');
}
function loopAge() {
setTimeout(showTotalAge, 1000);
}
loopAge();
$(".js-birthday").change(function () {
var vArr = $(this).val().split('-');
bd = new Date(vArr[0], parseInt(vArr[1], 10) - 1, vArr[2]);
bd.setHours(0);
bd.setMinutes(0);
bd.setSeconds(0);
console.log(bd);
});
Date
object and check whatgetTimezoneOffset
returns. Although it seems odd to me, it should happen in October - for European countries, at least. – MaxArt-60
– André Snede