Fixed date utils and added typeDoc/comments

This commit is contained in:
Giuseppe Digilio
2019-02-18 13:30:34 +01:00
parent dc6709c2b5
commit 18eaef3c2b
4 changed files with 49 additions and 17 deletions

View File

@@ -1,19 +1,39 @@
import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
import { isObject } from 'lodash';
import * as moment from 'moment';
export function isDateObject(value) {
/**
* Returns true if the passed value is a NgbDateStruct.
*
* @param value
* The object to check
* @return boolean
* true if the passed value is a NgbDateStruct, false otherwise
*/
export function isNgbDateStruct(value: object): boolean {
return isObject(value) && value.hasOwnProperty('day')
&& value.hasOwnProperty('month') && value.hasOwnProperty('year');
}
export function dateToGMTString(date: Date | NgbDateStruct) {
let year = ((date instanceof Date) ? date.getFullYear() : date.year).toString();
let month = ((date instanceof Date) ? date.getMonth() + 1 : date.month).toString();
let day = ((date instanceof Date) ? date.getDate() : date.day).toString();
let hour = ((date instanceof Date) ? date.getHours() : 0).toString();
let min = ((date instanceof Date) ? date.getMinutes() : 0).toString();
let sec = ((date instanceof Date) ? date.getSeconds() : 0).toString();
/**
* Returns a date in simplified extended ISO format (YYYY-MM-DDTHH:mm:ssZ).
* The timezone is always zero UTC offset, as denoted by the suffix "Z"
*
* @param date
* The date to format
* @return string
* the formatted date
*/
export function dateToISOFormat(date: Date | NgbDateStruct): string {
const dateObj: Date = (date instanceof Date) ? date : ngbDateStructToDate(date);
let year = dateObj.getFullYear().toString();
let month = (dateObj.getMonth() + 1).toString();
let day = dateObj.getDate().toString();
let hour = dateObj.getHours().toString();
let min = dateObj.getMinutes().toString();
let sec = dateObj.getSeconds().toString();
year = (year.length === 1) ? '0' + year : year;
month = (month.length === 1) ? '0' + month : month;
@@ -21,6 +41,18 @@ export function dateToGMTString(date: Date | NgbDateStruct) {
hour = (hour.length === 1) ? '0' + hour : hour;
min = (min.length === 1) ? '0' + min : min;
sec = (sec.length === 1) ? '0' + sec : sec;
return `${year}-${month}-${day}T${hour}:${min}:${sec}Z`;
const dateStr = `${year}${month}${day}${hour}${min}${sec}`;
return moment.utc(dateStr, 'YYYYMMDDhhmmss').format();
}
/**
* Returns a Date object started from a NgbDateStruct object
*
* @param date
* The NgbDateStruct to convert
* @return Date
* the Date object
*/
export function ngbDateStructToDate(date: NgbDateStruct): Date {
return new Date(date.year, (date.month - 1), date.day);
}