Skip to content

Commit

Permalink
Fikset så vi også viser UTC-datoer med riktig lokal tid
Browse files Browse the repository at this point in the history
  • Loading branch information
gruble committed May 16, 2024
1 parent 6ad9574 commit ab30f6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnChanges } from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { GeoHazard } from 'src/app/modules/common-core/models';
import { settings } from '../../../../settings';
import {
Expand Down Expand Up @@ -37,6 +37,7 @@ import {
ConfirmationModalService,
PopupResponse,
} from '../../../core/services/confirmation-modal/confirmation-modal.service';
import { isoDateTimeToLocalDateTimeInIsoFormat } from 'src/app/modules/common-core/helpers/date-converters';

const DEBUG_TAG = 'ObservationListCardComponent';
const FETCH_OBS_TIMEOUT_MS = 5000;
Expand Down Expand Up @@ -98,10 +99,10 @@ export class ObservationListCardComponent implements OnChanges {
this.obsTime = this.obs.DtObsTime;
if (!Capacitor.isNativePlatform()) {
// Vis registrert- og evt. endret-tidspunkt på web
this.regTime = this.obs.DtRegTime;
this.regTime = isoDateTimeToLocalDateTimeInIsoFormat(this.obs.DtRegTime);
if (this.obs.DtChangeTime && this.obs.DtChangeTime !== this.obs.DtRegTime) {
// Vis endret-tidspunkt kun hvis observasjonen er endret
this.changedTime = this.obs.DtChangeTime;
this.changedTime = isoDateTimeToLocalDateTimeInIsoFormat(this.obs.DtChangeTime);
}
}
this.icon = this.getGeoHazardCircleIcon(this.geoHazard);
Expand Down
25 changes: 25 additions & 0 deletions src/app/modules/common-core/helpers/date-converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,28 @@ export function convertToIsoDateTime(date: string, start: 'start' | 'end' = 'sta
const momentOut = momentIn[start + 'Of']('day');
return momentOut.toISOString(true);
}

/**
* Konverterer en ISO-tidsstreng med offset til lokal tid i ISO-format.
* Eksempel: 2021-09-01T09:00:00+00:00 -> 2021-09-01T11:00:00+02:00
*/
export function isoDateTimeToLocalDateTimeInIsoFormat(isoDateTimeWithOffset: string): string {
const date = new Date(isoDateTimeWithOffset);

const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');

const timeZoneOffsetInMinutes = date.getTimezoneOffset();
const timeZoneOffsetHours = Math.floor(timeZoneOffsetInMinutes / 60);
const sign = timeZoneOffsetHours > 0 ? '-' : '+';
const remainingMinutes = Math.abs(timeZoneOffsetInMinutes % 60);
const offset = `${Math.abs(timeZoneOffsetHours).toString().padStart(2, '0')}:${remainingMinutes.toString().padStart(2, '0')}`;

return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}${sign}${offset}`;
}


0 comments on commit ab30f6f

Please sign in to comment.