ng-jhipster
Version:
A Jhipster util library for Angular 2
63 lines (62 loc) • 1.87 kB
JavaScript
import { DatePipe } from '@angular/common';
import { Injectable } from '@angular/core';
/**
* An utility service for date.
*/
export var DateUtils = (function () {
function DateUtils(datePipe) {
this.datePipe = datePipe;
this.pattern = 'yyyy-MM-dd';
}
/**
* Method to convert the date time from server into JS date object
*/
DateUtils.prototype.convertDateTimeFromServer = function (date) {
if (date) {
return new Date(date);
}
else {
return null;
}
};
/**
* Method to convert the date from server into JS date object
*/
DateUtils.prototype.convertLocalDateFromServer = function (date) {
if (date) {
var dateString = date.split('-');
return new Date(dateString[0], dateString[1] - 1, dateString[2]);
}
return null;
};
/**
* Method to convert the JS date object into specified date pattern
*/
DateUtils.prototype.convertLocalDateToServer = function (date, pattern) {
if (pattern === void 0) { pattern = this.pattern; }
if (date) {
return this.datePipe.transform(date, pattern);
}
else {
return null;
}
};
/**
* Method to get the default date pattern
*/
DateUtils.prototype.dateformat = function () {
return this.pattern;
};
// TODO Find a better way to have Date fields because NgbDatePicker returns an object (Related to entities)
DateUtils.prototype.toDate = function (date) {
return date ? new Date(date.year, date.month - 1, date.day) : null;
};
DateUtils.decorators = [
{ type: Injectable },
];
/** @nocollapse */
DateUtils.ctorParameters = function () { return [
{ type: DatePipe, },
]; };
return DateUtils;
}());