From: Bill Erickson Date: Fri, 23 Aug 2019 20:20:39 +0000 (-0400) Subject: LP1840782 Date function to get timezone-aware parts X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=c03f859f486ced9a71892763753547796c27582b;p=working%2FEvergreen.git LP1840782 Date function to get timezone-aware parts Adds a function (and sandbox example) that returns a date chopped up into parts (year, month, day, hour, minute) matching the requested time zone. Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/src/eg2/src/app/share/util/date.ts b/Open-ILS/src/eg2/src/app/share/util/date.ts index bf146c7661..c8a9ffd5e7 100644 --- a/Open-ILS/src/eg2/src/app/share/util/date.ts +++ b/Open-ILS/src/eg2/src/app/share/util/date.ts @@ -7,6 +7,14 @@ interface DateFormatOptions { withTime?: boolean; // defaults to FALSE } +class DateParts { + year: number; + month: number; + day: number; + hour: number; + minute: number; +} + export class DateUtil { // Returns a YYYY-MM-DD string in the local time zone matching @@ -71,4 +79,38 @@ export class DateUtil { return new Intl.DateTimeFormat(locale, formatOps).format(date); } + + // Returns the each part of a date as descrete values from the + // provided date, translated into the requested time zone. + static dateToParts(date: Date, timeZone: string): DateParts { + + const formatOps: any = { + hour12: false, + year: 'numeric', + month: 'numeric', + day: 'numeric', + hour: 'numeric', + minute: 'numeric' + }; + + if (timeZone) { + // Defaults to browser time zone. + formatOps.timeZone = timeZone; + } + + // The locale "en-US" is used here because it allows the + // formatter to produce numeric values that are parseable by JS + // Date objects / components. + const formatter = Intl.DateTimeFormat('en-US', formatOps); + const parts = formatter.formatToParts(); + const response: DateParts = new DateParts(); + + ['year', 'month', 'day', 'hour', 'minute'].forEach(datePart => + response[datePart] = parts.filter(p => p.type === datePart)[0].value + ); + + return response; + } } + + diff --git a/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html b/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html index bf5e15e824..c0fb1cebe2 100644 --- a/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html +++ b/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html @@ -172,15 +172,16 @@ [(ngModel)]="withDate" (change)="setDateString()"/> - -
-
{{dateString}}
+
+
{{dateString}}
+
{{dateParts | json}}
+
diff --git a/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts b/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts index 69645be002..072965f0d9 100644 --- a/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.ts @@ -51,6 +51,7 @@ export class SandboxComponent implements OnInit { withDate = true; withTime = false; dateString: string; + dateParts: any; dateNow: string; // @ViewChild('helloStr') private helloStr: StringComponent; @@ -407,7 +408,9 @@ export class SandboxComponent implements OnInit { withTime: this.withTime }; - this.dateString = DateUtil.dateToLocaleString(new Date(), options); + const d = new Date(); + this.dateString = DateUtil.dateToLocaleString(d, options); + this.dateParts = DateUtil.dateToParts(d, this.dateTimeZone); } }