UNPKG

@entestat/formula

Version:

fast excel formula parser

74 lines (61 loc) 1.5 kB
const moment = require('moment-timezone'); const format = /^\d{4}-\d{2}-\d{2}$/ function pad(num, size) { var s = "000" + num; return s.substr(s.length-size); } class LocalDate{ /** * @param year {number?} * @param month {number?} * @param day {number?} */ constructor(year, month, day) { /** @type {string} format: 2021-01-29*/ this.value = null; if(year !== undefined && month !== undefined && day !== undefined){ this.set(year, month, day); } } asMoment(){ return moment(this.value); } _getPos(start, end){ if(this.value === null){ return null; } if(!this.value.match(format)){ return null; } return Number(this.value.substring(start,end)); } getYear(){ return this._getPos(0, 4); } getMonth(){ return this._getPos(5, 7); } getDay(){ return this._getPos(8, 10); } toJSON(){ return this.value; } fromMoment(momentDate){ this.set(momentDate.year(), momentDate.month() + 1, momentDate.date()); return this; } set(year, month, day){ this.value = pad(year, 4) + "-" + pad(month, 2) + "-" + pad(day, 2); let x =5; } toString(format){ if(this.value){ return moment(this.value).format(format); } else{ return ''; } } } module.exports = {LocalDate}