Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

import { Injectable } from '@angular/core';
import { NgbDateParserFormatter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';

@Injectable()
export class VADateParser extends NgbDateParserFormatter {
parse(value: string): NgbDateStruct {
if (value) {

// our acceptable formats are mm/dd/(yy)yy or mm-dd-(yy)yy
let dtfmt = null;
if (value.match(/^(0?[1-9]|1[012])[\/](0?[1-9]|[12][0-9]|3[01])[\/]\d{4}$/)) {
dtfmt = 1;
} else if (value.match(/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])$/)) {
dtfmt = 2;
} else if (value.match(/\bnow\b/gi)) { // now
dtfmt = 3;
} else if (value.match(/^t\x2b[0-9]+/gi)) { // t+n, t+1, or t+365
dtfmt = 4;
} else if (value.match(/^t\x2d[0-9]+/gi)) { // t-n, t-1, or t-365
dtfmt = 5;
} else if (value.match(/\bt\b/gi)) { // T or t for today's date
dtfmt = 6;
} else {
return null;
}

let dt = null;

// split the date up
const dateParts = value.trim().split(/[-\/]/);
switch (dtfmt) {
case 1:
if (dateParts[2].length === 2) {
dateParts[2] = (+dateParts[2] > 35) ? '19' + dateParts[2] : '20' + dateParts[2];
}
break;
case 2:
const tmp = dateParts[0];
dateParts[0] = dateParts[1];
dateParts[1] = dateParts[2];
dateParts[2] = tmp;
break;
case 3: // now
const now = new Date();
const nowMonth = now.getMonth() + 1;
const nowDay = now.getDate();
const nowYear = now.getFullYear();
dateParts[0] = nowMonth.toString();
dateParts[1] = nowDay.toString();
dateParts[2] = nowYear.toString();
break;
case 4: // T+n or t+1
const numberOfDaysPlus = value.trim().split(/[+]/);
const nodPlus = numberOfDaysPlus[1];
const nodIntPlus = parseInt(nodPlus, 10);
const todayPlus = new Date();
todayPlus.setDate(todayPlus.getDate() + nodIntPlus);
const theNewDatePlus = todayPlus;
const monthPlus = theNewDatePlus.getMonth() + 1;
const dayPlus = theNewDatePlus.getDate();
const yearPlus = theNewDatePlus.getFullYear();
dateParts[0] = monthPlus.toString();
dateParts[1] = dayPlus.toString();
dateParts[2] = yearPlus.toString();
break;
case 5: // T-n or t-1
const numberOfDaysMinus = value.trim().split(/[-]/);
const nodMinus = numberOfDaysMinus[1];
const nodIntMinus = parseInt(nodMinus, 10);
const todayMinus = new Date();
todayMinus.setDate(todayMinus.getDate() - nodIntMinus);
const theNewDate = todayMinus;
const monthMinus = theNewDate.getMonth() + 1;
const dayMinus = theNewDate.getDate();
const yearMinus = theNewDate.getFullYear();
dateParts[0] = monthMinus.toString();
dateParts[1] = dayMinus.toString();
dateParts[2] = yearMinus.toString();
break;
case 6: // T or t for today's date
const today = new Date();
const todayMonth = today.getMonth() + 1;
const todayDay = today.getDate();
const todayYear = today.getFullYear();
dateParts[0] = todayMonth.toString();
dateParts[1] = todayDay.toString();
dateParts[2] = todayYear.toString();
break;
default : return null;
}

// validate the date
dt = new Date(dateParts[0] + '/' + dateParts[1] + '/' + dateParts[2]);

if (dt.toString() === 'Invalid Date') { return null; }

return {year: +dateParts[2], month: +dateParts[0], day: +dateParts[1]};

}
return null;
}

format(date: NgbDateStruct): string {
return date ?
`${(typeof date.month === 'number') ? date.month : ''}/${(typeof date.day === 'number') ? date.day : ''}/${date.year}` :
'';
}
}