diff --git a/src/app/core/json-patch/builder/json-patch-operations-builder.ts b/src/app/core/json-patch/builder/json-patch-operations-builder.ts index af2ea3496c..c45183b4ef 100644 --- a/src/app/core/json-patch/builder/json-patch-operations-builder.ts +++ b/src/app/core/json-patch/builder/json-patch-operations-builder.ts @@ -8,7 +8,7 @@ import { import { JsonPatchOperationPathObject } from './json-patch-operation-path-combiner'; import { Injectable } from '@angular/core'; import { isEmpty, isNotEmpty } from '../../../shared/empty.util'; -import { dateToGMTString } from '../../../shared/date.util'; +import { dateToISOFormat } from '../../../shared/date.util'; import { AuthorityValue } from '../../integration/models/authority.value'; import { FormFieldMetadataValueObject } from '../../../shared/form/builder/models/form-field-metadata-value.model'; import { FormFieldLanguageValueObject } from '../../../shared/form/builder/models/form-field-language-value.model'; @@ -105,7 +105,7 @@ export class JsonPatchOperationsBuilder { if (isEmpty(value) || value instanceof FormFieldMetadataValueObject) { operationValue = value; } else if (value instanceof Date) { - operationValue = new FormFieldMetadataValueObject(dateToGMTString(value)); + operationValue = new FormFieldMetadataValueObject(dateToISOFormat(value)); } else if (value instanceof AuthorityValue) { operationValue = this.prepareAuthorityValue(value); } else if (value instanceof FormFieldLanguageValueObject) { diff --git a/src/app/shared/date.util.ts b/src/app/shared/date.util.ts index 8be4235643..5e91871967 100644 --- a/src/app/shared/date.util.ts +++ b/src/app/shared/date.util.ts @@ -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); } diff --git a/src/app/shared/form/builder/form-builder.service.ts b/src/app/shared/form/builder/form-builder.service.ts index 630e9280ff..21e702aabb 100644 --- a/src/app/shared/form/builder/form-builder.service.ts +++ b/src/app/shared/form/builder/form-builder.service.ts @@ -28,7 +28,7 @@ import { import { DynamicRowArrayModel } from './ds-dynamic-form-ui/models/ds-dynamic-row-array-model'; import { DsDynamicInputModel } from './ds-dynamic-form-ui/models/ds-dynamic-input.model'; import { FormFieldMetadataValueObject } from './models/form-field-metadata-value.model'; -import { isDateObject } from '../../date.util'; +import { isNgbDateStruct } from '../../date.util'; @Injectable() export class FormBuilderService extends DynamicFormService { @@ -109,7 +109,7 @@ export class FormBuilderService extends DynamicFormService { } else if (isObject(controlValue)) { const authority = controlValue.authority || controlValue.id || null; const place = controlModelIndex || controlValue.place; - if (isDateObject(controlValue)) { + if (isNgbDateStruct(controlValue)) { return new FormFieldMetadataValueObject(controlValue, controlLanguage, authority, controlValue, place); } else { return new FormFieldMetadataValueObject(controlValue.value, controlLanguage, authority, controlValue.display, place, controlValue.confidence); diff --git a/src/app/submission/sections/upload/file/file.component.ts b/src/app/submission/sections/upload/file/file.component.ts index f23358a09c..572e91cf2b 100644 --- a/src/app/submission/sections/upload/file/file.component.ts +++ b/src/app/submission/sections/upload/file/file.component.ts @@ -12,7 +12,7 @@ import { JsonPatchOperationPathCombiner } from '../../../../core/json-patch/buil import { WorkspaceitemSectionUploadFileObject } from '../../../../core/submission/models/workspaceitem-section-upload-file.model'; import { SubmissionFormsModel } from '../../../../core/config/models/config-submission-forms.model'; import { deleteProperty } from '../../../../shared/object.util'; -import { dateToGMTString } from '../../../../shared/date.util'; +import { dateToISOFormat } from '../../../../shared/date.util'; import { SubmissionService } from '../../../submission.service'; import { FileService } from '../../../../core/shared/file.service'; import { HALEndpointService } from '../../../../core/shared/hal-endpoint.service'; @@ -144,12 +144,12 @@ export class UploadSectionFileComponent implements OnChanges, OnInit { accessConditionOpt.groupUUID = this.retrieveValueFromField(accessCondition.groupUUID); if (accessCondition.startDate) { const startDate = this.retrieveValueFromField(accessCondition.startDate); - accessConditionOpt.startDate = dateToGMTString(startDate); + accessConditionOpt.startDate = dateToISOFormat(startDate); accessConditionOpt = deleteProperty(accessConditionOpt, 'endDate'); } if (accessCondition.endDate) { const endDate = this.retrieveValueFromField(accessCondition.endDate); - accessConditionOpt.endDate = dateToGMTString(endDate); + accessConditionOpt.endDate = dateToISOFormat(endDate); accessConditionOpt = deleteProperty(accessConditionOpt, 'startDate'); } accessConditionsToSave.push(accessConditionOpt);