ng-jhipster
Version:
A Jhipster util library for Angular 2
118 lines (113 loc) • 4.03 kB
JavaScript
/*
Copyright 2013-2017 the original author or authors from the JHipster project.
This file is part of the JHipster project, see https://jhipster.github.io/
for more information.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { DatePipe } from '@angular/common';
import { Injectable } from '@angular/core';
/**
* An utility service for date.
*/
var JhiDateUtils = (function () {
function JhiDateUtils() {
this.pattern = 'yyyy-MM-dd';
this.datePipe = new DatePipe('en');
}
/**
* Method to convert the date time from server into JS date object
*/
/**
* Method to convert the date time from server into JS date object
*/
JhiDateUtils.prototype.convertDateTimeFromServer = /**
* Method to convert the date time from server into JS date object
*/
function (date) {
if (date) {
return new Date(date);
}
else {
return null;
}
};
/**
* Method to convert the date from server into JS date object
*/
/**
* Method to convert the date from server into JS date object
*/
JhiDateUtils.prototype.convertLocalDateFromServer = /**
* Method to convert the date from server into JS date object
*/
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
*/
/**
* Method to convert the JS date object into specified date pattern
*/
JhiDateUtils.prototype.convertLocalDateToServer = /**
* Method to convert the JS date object into specified date pattern
*/
function (date, pattern) {
if (pattern === void 0) { pattern = this.pattern; }
if (date) {
var newDate = new Date(date.year, date.month - 1, date.day);
return this.datePipe.transform(newDate, pattern);
}
else {
return null;
}
};
/**
* Method to get the default date pattern
*/
/**
* Method to get the default date pattern
*/
JhiDateUtils.prototype.dateformat = /**
* Method to get the default date pattern
*/
function () {
return this.pattern;
};
// TODO Change this method when moving from datetime-local input to NgbDatePicker
// TODO Change this method when moving from datetime-local input to NgbDatePicker
JhiDateUtils.prototype.toDate =
// TODO Change this method when moving from datetime-local input to NgbDatePicker
function (date) {
if (date === undefined || date === null) {
return null;
}
var dateParts = date.split(/\D+/);
if (dateParts.length === 7) {
return new Date(dateParts[0], dateParts[1] - 1, dateParts[2], dateParts[3], dateParts[4], dateParts[5], dateParts[6]);
}
if (dateParts.length === 6) {
return new Date(dateParts[0], dateParts[1] - 1, dateParts[2], dateParts[3], dateParts[4], dateParts[5]);
}
return new Date(dateParts[0], dateParts[1] - 1, dateParts[2], dateParts[3], dateParts[4]);
};
JhiDateUtils.decorators = [
{ type: Injectable },
];
/** @nocollapse */
JhiDateUtils.ctorParameters = function () { return []; };
return JhiDateUtils;
}());
export { JhiDateUtils };