Skip to content

Instantly share code, notes, and snippets.

@oscarcs
Last active May 31, 2021 02:38
Show Gist options
  • Save oscarcs/8efb58b1b5c424be51a46621d9880646 to your computer and use it in GitHub Desktop.
Save oscarcs/8efb58b1b5c424be51a46621d9880646 to your computer and use it in GitHub Desktop.
"use strict";
/**
* Create a new CustomDate object.
* @param {Number} year 4-digit year
* @param {Number} month 1-indexed month (1 = January, etc.)
* @param {Number} day Day of the month (1-31)
*/
let CustomDate = function(year, month, day) {
// Set the defaults:
let today = new Date();
this.day = today.getDate();
this.month = today.getMonth() + 1;
this.year = today.getFullYear();
if (year instanceof CustomDate) {
let d = year;
this.day = d.day;
this.month = d.month;
this.year = d.year;
}
else {
if (typeof day !== 'undefined') {
this.day = day;
}
if (typeof month !== 'undefined') {
this.month = month;
}
if (typeof year !== 'undefined') {
this.year = year;
}
}
function pad(value, numDigits) {
value = value + '';
while (value.length < numDigits) {
value = '0' + value;
}
return value;
}
this.setDay = function(day) {
this.day = day;
if (this.day > this.getDaysInMonth()) {
this.day = this.getDaysInMonth();
}
if (this.day < 1) {
this.day = 1;
}
return this;
};
/**
* Add (or subtract) a certain number of days to this date.
*/
this.addDays = function(num) {
this.day += num;
if (num < 0) {
while (this.day <= 0) {
this.month--;
if (this.month < 1) {
this.year--;
this.month = 12;
}
this.day = this.getDaysInMonth() + this.day;
}
}
else {
while (this.day > this.getDaysInMonth()) {
this.day = this.day - this.getDaysInMonth();
this.month++;
if (this.month > 12) {
this.year++;
this.month = 1;
}
}
}
return this;
};
this.setMonth = function(month) {
this.month = month;
if (this.month > 12) {
this.month = 12;
}
if (this.month < 1) {
this.month = 1;
}
return this;
};
/**
* Add (or subtract) a certain number of months to this date.
*/
this.addMonths = function(num) {
this.month += num;
if (num < 0) {
while (this.month <= 0) {
this.month += 12;
this.year--;
}
}
else if (num > 0) {
while (this.month > 12) {
this.month -= 12;
this.year++;
}
}
// If the new month has fewer days than the original, use the last day.
if (this.day > this.getDaysInMonth()) {
this.day = this.getDaysInMonth();
}
return this;
};
this.setYear = function(year) {
this.year = year;
return this;
};
/**
* Add a certain number of years to this date.
*/
this.addYears = function(num) {
this.year += num;
// Handle leap years:
if (this.day > this.getDaysInMonth()) {
this.day = this.getDaysInMonth();
}
return this;
};
/**
* Get the Javascript Date() representation of this date.
*/
this.toDate = function() {
let d = new Date();
d.setFullYear(this.year);
d.setMonth(this.month - 1, this.day);
d.setDate(this.day);
return d;
};
/**
* Get a numeric representation of this date as a day of the week,
* where 1 = Monday and 7 = Sunday.
*/
this.getDayOfWeek = function() {
// Wrap 0 (Sunday in js representation) to 7.
let day = this.toDate().getDay();
return day === 0 ? 7 : day;
};
/**
* Get the number of days in the month specified by this date.
*/
this.getDaysInMonth = function() {
return new Date(this.year, this.month, 0).getDate();
};
/**
* Get the day of the week as a string for display.
*/
this.getDisplayDayOfWeek = function() {
switch (this.getDayOfWeek()) {
case 1: return 'Monday';
case 2: return 'Tuesday';
case 3: return 'Wednesday';
case 4: return 'Thursday';
case 5: return 'Friday';
case 6: return 'Saturday';
case 7: return 'Sunday';
}
};
this.getDisplayDayOfWeekThreeLetter = function() {
switch (this.getDayOfWeek()) {
case 1: return 'Mon';
case 2: return 'Tue';
case 3: return 'Wed';
case 4: return 'Thu';
case 5: return 'Fri';
case 6: return 'Sat';
case 7: return 'Sun';
}
};
/**
* Get the month as a string for display.
*/
this.getDisplayMonth = function() {
switch (this.month) {
case 1: return 'January';
case 2: return 'February';
case 3: return 'March';
case 4: return 'April';
case 5: return 'May';
case 6: return 'June';
case 7: return 'July';
case 8: return 'August';
case 9: return 'September';
case 10: return 'October';
case 11: return 'November';
case 12: return 'December';
}
};
/**
* Get the ISO 8601 display representation of this date.
*/
this.to8601 = function() {
return pad(this.year, 4) + '-' + pad(this.month, 2) + '-' + pad(this.day, 2);
};
/**
* Get the NZ customary display representation of this date.
* e.g. 01/12/2001
*/
this.toNZ = function() {
return pad(this.day, 2) + '/' + pad(this.month, 2) + '/' + this.year;
};
/**
* Get the full NZ customary display representation of this date.
* e.g. Saturday, 1 December 2001
*/
this.toNZFull = function() {
return this.getDisplayDayOfWeek() + ', ' + this.day + ' ' + this.getDisplayMonth() + ' ' + this.year;
};
/**
* Parse a datestring in ISO 8601 format.
*/
this.from8601 = function(str) {
if (str === '' || typeof str === 'undefined') return;
let parts = str.split('-');
this.year = parseInt(parts[0]);
this.month = parseInt(parts[1]);
this.day = parseInt(parts[2]);
return this;
};
/**
* Parse a datestring in NZ customary format.
*/
this.fromNZ = function(str) {
if (str === '' || typeof str === 'undefined') return;
let parts = str.split('/');
if (parts.length < 3 || parts[0] === '' || parts[1] === '' || parts[2] === '') return;
this.day = parseInt(parts[0]);
this.month = parseInt(parts[1]);
this.year = parseInt(parts[2]);
return this;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment