For commercial projects, we create a code module call util to handle all the date conversions using moment.js, then call those functions.
More on moment.js here:
https://momentjs.com/docs/
Here’s a snippet of our util functions:
// Date conversion functions
DDMMYYtoDDMMYYYY(d) {
return (d === '') ? null : moment(d, 'DD/MM/YY').format('DD/MM/YYYY');
},
DDMMYYYYtoYYYYMMDD(d) {
return (d === '') ? null : moment.utc(moment(d, 'DD/MM/YYYY')).format('YYYY/MM/DD');
},
YMM00toDDMMYY(d) {
return (d === '') ? null : moment(d, 'YYMM').endOf('month').format('DD/MM/YY');
},
dateToDDMMYYYY(d) {
return (d === null) ? '' : moment.utc(d).format('DD/MM/YYYY');
},
dateToDDMMYY(d) {
return (d === null) ? '' : moment.utc(d).format('DD/MM/YY');
},
dateToDDMM(d) {
return (d === null) ? '' : moment.utc(d).format('DD/MM');
},
dateToYYMMDD(d) {
return (d === null) ? '' : moment.utc(d).format('YYMMDD');
},
dateToLocalDateTime(d) {
return (d === null) ? '' : moment(d).format('DD/MM/YYYY hh:mm a');
},
dateTimeStamp(d) {
return (d === null) ? '' : moment(d).valueOf();
},
DDMMYYToDate(d) {
return (d === '') ? null : moment.utc(d, 'DD/MM/YY').toDate();
},
DDMMYYYYToDate(d) {
return (d === '') ? null : moment.utc(d, 'DD/MM/YYYY').toDate();
},
startOfMonth(d) {
return moment(d, 'YYMM').toDate();
},
endOfMonth(d) {
return moment(d, 'YYMM').endOf('month').toDate();
},
monthShortName(d) {
return moment(d).format('MMM');
},
dateJStoDB(d) {
// converts a DD/MM/YY string to an ISOString suitable for SQLite.
if (d === null) return null;
if (!d.toISOString) {
return (d === '') ? null : util.DDMMYYToDate(d).toISOString();
}
return d.toISOString();
},
dateDBtoJS(d) {
return (d === null) ? null : moment.utc(d).toDate();
},
dateNowUTC() {
return moment.utc({ hour: 0 });
},
dateNow() {
return util.dateNowUTC().toDate();
},
dateNowDDMMYY() {
return util.dateNowUTC().format('DD/MM/YY');
},
dateNowDDMMYYYY() {
return util.dateNowUTC().format('DD/MM/YYYY');
},
dateNowYYYYww() {
return util.dateNowUTC().format('YYYYww');
},
dateNowDateTime() {
return util.dateNowUTC().format('DD/MM/YYYY hh:mm a');
},
DDMMYYYYToDateNoUTC(d) {
// used in datepicker.js. Pickadate wants non UTC date.
return (d === '') ? null : moment(d, 'DD/MM/YYYY').toDate();
},
dateAgo(d) {
// returns days ago from current date
return moment.utc().startOf('day').diff(d, 'days');
},
dateInFuture(d) {
// checks if DDMMYY is in future
if (d === '') return null;
return (moment.utc().diff(util.DDMMYYToDate(d)) < 0);
},
dateAddDays(d, days) {
if (d === '') return null;
return moment.utc(d).add(days, 'days').toDate();
// Should be the same as DateAdd("d",d1) in BASIC - if not, please correct
},
dateAddToDDMMYY(days) {
const today = util.dateNow();
return util.dateToDDMMYY(util.dateAddDays(today, days));
},
dateAddMonths(d, months) {
if (d === '') return null;
return moment.utc(d).add(months, 'months').toDate();
// Should be the same as DateAdd("m",d1) in BASIC - if not, please correct
},
dateDifferenceDays(d1, d2) {
return moment.utc(d2).startOf('day').diff(moment.utc(d1).startOf('day'), 'days');
},
dateStartOfYear() {
return moment.utc().startOf('year');
},
dateSetValues(el, d) {
// set the value in DDMMYY format, and data-value for pickadate.
// eslint-disable-next-line no-param-reassign
el.value = d;
if (!d) {
el.setAttribute('data-value', '');
} else {
el.setAttribute('data-value', util.DDMMYYtoDDMMYYYY(el.value));
}
},
dayOfWeek() {
return util.dateNowUTC().isoWeekday(); // 1 - Monday ... 7 - Sunday
},