Added new date utils

This commit is contained in:
Giuseppe Digilio
2020-04-06 16:27:58 +02:00
parent 9339ae2d7c
commit 5cb0570bc0

View File

@@ -3,6 +3,8 @@ import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
import { isObject } from 'lodash';
import * as moment from 'moment';
import { isEmpty } from './empty.util';
/**
* Returns true if the passed value is a NgbDateStruct.
*
@@ -13,7 +15,7 @@ import * as moment from 'moment';
*/
export function isNgbDateStruct(value: object): boolean {
return isObject(value) && value.hasOwnProperty('day')
&& value.hasOwnProperty('month') && value.hasOwnProperty('year');
&& value.hasOwnProperty('month') && value.hasOwnProperty('year');
}
/**
@@ -56,3 +58,35 @@ export function dateToISOFormat(date: Date | NgbDateStruct): string {
export function ngbDateStructToDate(date: NgbDateStruct): Date {
return new Date(date.year, (date.month - 1), date.day);
}
/**
* Returns a NgbDateStruct object started from a string representing a date
*
* @param date
* The Date to convert
* @return NgbDateStruct
* the NgbDateStruct object
*/
export function stringToNgbDateStruct(date: string): NgbDateStruct {
return dateToNgbDateStruct(new Date(date));
}
/**
* Returns a NgbDateStruct object started from a Date object
*
* @param date
* The Date to convert
* @return NgbDateStruct
* the NgbDateStruct object
*/
export function dateToNgbDateStruct(date?: Date): NgbDateStruct {
if (isEmpty(date)) {
date = new Date()
}
return {
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate()
};
}