Skip to content

Commit

Permalink
Date related functions
Browse files Browse the repository at this point in the history
Date related functions
  • Loading branch information
ace-coder committed Apr 6, 2015
1 parent e2e988d commit 13834d9
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function calcAge(dateString) {
var birthday = +new Date(dateString);
return ~~((Date.now() - birthday) / (31557600000));
}
function monthDiff(d1) {
d1 = new Date(d1);
var d2 = new Date();
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
function daysDiff(date1) {
var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(date1);
var secondDate = new Date();

var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime()) / (oneDay)));
return diffDays;
}
function isValidDate(dt) {
if (Object.prototype.toString.call(d) === "[object Date]") {
// it is a date
if (isNaN(d.getTime())) { // d.valueOf() could also work
// date is not valid
}
else {
// date is valid
}
}
else {
// not a date
}
}

function isDate(sDate) {
var scratch = new Date(sDate);
if (scratch.toString() == "NaN" || scratch.toString() == "Invalid Date") {
return false;
} else {
return true;

}
}

0 comments on commit 13834d9

Please sign in to comment.