mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-16 14:33:03 +00:00
Added new date utils
This commit is contained in:
@@ -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()
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user