-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Date related functions
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} |