The stringdate.js provides functions handling String as a Date.
Valid format is "yyyy-MM-dd" in general. "yyyy-MM" is allowed for getLastDate() and isLeapYear(). "yyyy" is allowed for isLeapYear() only.
Note that the string should be valid syntactically and semantically. It means "2015-04-31" is not valid in stringdate.js while it makes a date of May 1st, 2015 in vanilla JavaScript.
All functions in below should applied on valid format, otherwise it throws Error.
String.prototype.getYear()
: Returns year of 4-digits.
String.prototype.getMonth()
: Returns month between 1 and 12.
: Note that vanilla JavaScript Date object's getMonth() returns integer between 0 and 11.
String.prototype.getDate()
: Returns date of month between 1 and 31.
// get year
"2015-03-14".getYear(); // 2015
// get month
"2015-03-14".getMonth(); // 3
// get date of month
"2015-03-14".getYear(); // 14
// get day of week
"2015-03-14".getDay(); // 6String.prototype.getLastDate()
: Returns last date of the month. Date format can be either "yyyy-MM-dd" or "yyyy-MM".
String.prototype.isLeapYear()
: Determine whether the year is leap year or not. Date format can be one of "yyyy-MM-dd", "yyyy-MM", or "yyyy".
// get last date
"2015-02-01".getLastDate(); // 28
"2015-02".getLastDate(); // 28
// determine leap year
// 2015
"2015-02-01".isLeapYear(); // false
"2015-02".isLeapYear(); // false
"2015".isLeapYear(); // false
// 2016
"2016-02-01".isLeapYear(); // true
"2016-02".isLeapYear(); // true
"2016".isLeapYear(); // trueString.prototype.getDaysFrom(target)
: Returns days between this date and target date. It returns positive when target is past, or negative when target is future. You can use this function as subtraction operator -.
String.prototype.addDays(days)
: Returns new date after/before the date. It returns future date for positive days, or past date for negative days. You can use this function as addition operator +.
// get days between two dates
"2015-03-31".getDaysFrom("2015-03-01"); // 30
"2015-04-01".getDaysFrom("2015-06-30"); // -90
// get date after/before a date
"2015-03-14".addDays(100); // "2015-06-22"
"2015-03-14".addDays(-28); // "2015-02-14"var count = 0;
for (var date = "2015-04-01"; date <= "2015-06-30"; date = date.addDays(1))
if (date.getDay() != 0 && date.getDay() != 6)
count++;
console.log(count); // 65