Sunday, April 22, 2012

Javascript Dates

Had a little trouble with dates in Javascript until I remembered that everything dates from 1970.
var today = new Date();
console.log(today);
console.log("fullyear: " + today.getFullYear());
console.log("month: " + today.getMonth());
console.log("date: " + today.getDate());
console.log("time: " + today.getTime());
var bday = new Date(1956, 1, 4);
console.log("birthday: " + bday);

var z = new Date(new Date(1970, 0, 1) - bday.getTime());
console.log("difference-fullyear: " + z.getFullYear());
console.log("difference-month: " + z.getMonth());

var m = today.getFullYear() - bday.getFullYear();
console.log('years: ' + m);

m = today.getMonth() - bday.getMonth();
console.log('months: ' + m);

m = today.getDate() - bday.getDate();
console.log('days: ' + m);

Mon, 23 Apr 2012 04:12:30 GMT
fullyear: 2012
month: 3
date: 22
time: 1335154350175
birthday: Sat Feb 04 1956 00:00:00 GMT-0600 (CST)
difference-fullyear: 1983
difference-month: 10
years: 56
months: 2
days: 18