Skip to content

Instantly share code, notes, and snippets.

@tlimpanont
Last active August 31, 2016 20:22
Show Gist options
  • Save tlimpanont/40e00cb33e216931929a6f20baad97f0 to your computer and use it in GitHub Desktop.
Save tlimpanont/40e00cb33e216931929a6f20baad97f0 to your computer and use it in GitHub Desktop.
/**
* @class HamisDateTimePickerService - helper class for formatting and rendering DateTime
* ====Business rules====
input format is “dd MMM yyyy HH:mm” is ook display format.
ddHHmm en HHmm is ook input, tijd is altijd in de toekomst
of huidige tijd
dus als het volledig is tijd met “:” in de korte uitvoering zonder
071200 wordt 07 sep 2016 12:00
1200 wordt 31 aug 2016 12:00'
*/
class HamisDateTimePickerService {
fullInputDateTimeFormat = 'DD MMM YYYY HH:mm';
shortDateTimeFormat = 'DDHHmm';
timeFormat = 'HHmm';
displayDateTimeFormat = this.fullInputDateTimeFormat;
displayThisYearDateTimeFormat = 'DD MMM HH:mm';
static UNKNOWN_HAMIS_DATETIME_FORMAT: 'UNKNOWN_HAMIS_DATETIME_FORMAT';
constructor() {
moment.updateLocale('nl', {
monthsShort: 'jan_feb_mrt_apr_mei_jun_jul_aug_sep_okt_nov_dec'.split('_')
});
}
/**
* @param {moment} momentDate
* @return {string}
* if the passed date is in current year
* then return displayThisYearDateTimeFormat
* else return displayDateTimeFormat
*/
getCorrectRenderFormat(momentDate: moment): string {
return momentDate._d.getFullYear() === new Date().getFullYear() ?
this.displayThisYearDateTimeFormat : this.displayDateTimeFormat;
}
/**
* Check whether the inputString is a shortDateTimeFormat
* @param {string} inputString
* @return {boolean}
*/
isShortTimeDateFormat(inputString: string): boolean {
return Number(inputString) !== NaN
&& inputString.trim().length === 6
&& moment(inputString, this.shortDateTimeFormat).isValid()
}
/**
* Check whether the inputString is a timeFormat
* @param {string} inputString
* @return {boolean}
*/
isTimeFormat(inputString: string): boolean {
return Number(inputString) !== NaN
&& inputString.trim().length === 4
&& moment(inputString, this.timeFormat).isValid()
}
/**
* Check whether the inputString is a fullInputDateTimeFormat
* @param {string} inputString
* @return {boolean}
*/
isFullInputDateTimeFormat(inputString: string): boolean {
return /[0-9]{2}\s[a-z]{3}\s[0-9]{4}\s[0-9]{2}:[0-9]{2}/.test(inputString)
&& moment(inputString, this.fullInputDateTimeFormat).isValid()
}
/**
* Detect and parse format based on the given inputString
* @param {string} inputString
* @return {string}
*/
parseFormat(inputString: string): string {
if(this.isShortTimeDateFormat(inputString)) {
return this.shortDateTimeFormat;
}
else if(this.isTimeFormat(inputString)) {
return this.timeFormat;
}
else if(this.isFullInputDateTimeFormat(inputString)) {
return this.fullInputDateTimeFormat;
}
else {
throw new Error(this.UNKNOWN_HAMIS_DATETIME_FORMAT);
}
}
/**
* Render to the correct hamis datetime format
* this method includes the logic to correct a date
* which is set in future dateTime
* @param {string} inputString
* @return {string}
*/
renderToHamisFormat(inputString: string): string {
var inputMomentDate:moment = this.hamisMoment(inputString);
return inputMomentDate.format(this.getCorrectRenderFormat(inputMomentDate))
}
/**
* Create a moment date object with respect to the business rules
* @param {string} inputString
* @return {moment}
*/
hamisMoment(inputString: string): moment {
let inputFormat:string;
try {
inputFormat = this.parseFormat(inputString);
} catch (e) {
return moment();
}
let inputMomentDate:moment = moment(inputString, inputFormat);
if(inputFormat === this.shortDateTimeFormat) {
inputMomentDate = (inputMomentDate.isSameOrBefore(moment())) ?
inputMomentDate.add(1, 'months') : inputMomentDate;
}
else if(inputFormat === this.timeFormat) {
inputMomentDate = (inputMomentDate.isSameOrBefore(moment())) ?
inputMomentDate.add(1, 'days') : inputMomentDate;
}
return inputMomentDate;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment