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
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;
+ }
}
+
+
[(ngModel)]="withDate" (change)="setDateString()"/>
<label class="form-check-label">Include Date</label>
</div>
- </div>
- <div class="col-lg-2">
<div class="form-check">
<input class="form-check-input" type="checkbox"
[(ngModel)]="withTime" (change)="setDateString()"/>
<label class="form-check-label">Include Time</label>
</div>
</div>
- <div class="col-lg-4 font-weight-bold text-primary">{{dateString}}</div>
+ <div class="col-lg-6 font-weight-bold text-primary">
+ <div>{{dateString}}</div>
+ <div>{{dateParts | json}}</div>
+ </div>
</div>
</div>
withDate = true;
withTime = false;
dateString: string;
+ dateParts: any;
dateNow: string;
// @ViewChild('helloStr') private helloStr: StringComponent;
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);
}
}