Changes
This commit is contained in:
@@ -58,10 +58,14 @@
|
||||
<div class="col-md-6 mb-3">
|
||||
<label asp-for="Input.StartUtc" class="form-label"></label>
|
||||
<input asp-for="Input.StartUtc" class="form-control" type="datetime-local" />
|
||||
<span asp-validation-for="Input.StartUtc" class="text-danger"></span>
|
||||
<div id="startUtcDisplay" class="form-text"></div>
|
||||
</div>
|
||||
<div class="col-md-6 mb-3">
|
||||
<label asp-for="Input.EndUtc" class="form-label"></label>
|
||||
<input asp-for="Input.EndUtc" class="form-control" type="datetime-local" />
|
||||
<span asp-validation-for="Input.EndUtc" class="text-danger"></span>
|
||||
<div id="endUtcDisplay" class="form-text"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -122,3 +126,109 @@
|
||||
</form>
|
||||
|
||||
<partial name="_ValidationScriptsPartial" />
|
||||
<script>
|
||||
(function () {
|
||||
const startInput = document.getElementById("Input_StartUtc");
|
||||
const endInput = document.getElementById("Input_EndUtc");
|
||||
const startUtcDisplay = document.getElementById("startUtcDisplay");
|
||||
const endUtcDisplay = document.getElementById("endUtcDisplay");
|
||||
|
||||
function getOffsetMinutes(timeZone, instant) {
|
||||
const offsetPart = new Intl.DateTimeFormat("en-US", {
|
||||
timeZone: timeZone,
|
||||
timeZoneName: "shortOffset"
|
||||
})
|
||||
.formatToParts(instant)
|
||||
.find(function (part) { return part.type === "timeZoneName"; });
|
||||
|
||||
if (!offsetPart) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const match = offsetPart.value.match(/GMT([+-]\d{1,2})(?::(\d{2}))?/);
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const hours = parseInt(match[1], 10);
|
||||
const minutes = match[2] ? parseInt(match[2], 10) : 0;
|
||||
const sign = hours < 0 ? -1 : 1;
|
||||
return (Math.abs(hours) * 60 + minutes) * sign;
|
||||
}
|
||||
|
||||
function easternLocalToUtcInstant(localValue) {
|
||||
const match = localValue.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/);
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const year = Number(match[1]);
|
||||
const monthIndex = Number(match[2]) - 1;
|
||||
const day = Number(match[3]);
|
||||
const hour = Number(match[4]);
|
||||
const minute = Number(match[5]);
|
||||
|
||||
const naiveUtcMillis = Date.UTC(year, monthIndex, day, hour, minute, 0, 0);
|
||||
let guess = new Date(naiveUtcMillis);
|
||||
const offsetOne = getOffsetMinutes("America/New_York", guess);
|
||||
if (offsetOne === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let utcMillis = naiveUtcMillis - (offsetOne * 60 * 1000);
|
||||
guess = new Date(utcMillis);
|
||||
const offsetTwo = getOffsetMinutes("America/New_York", guess);
|
||||
if (offsetTwo !== null && offsetTwo !== offsetOne) {
|
||||
utcMillis = naiveUtcMillis - (offsetTwo * 60 * 1000);
|
||||
}
|
||||
|
||||
return new Date(utcMillis);
|
||||
}
|
||||
|
||||
function toUtcText(easternLocalValue) {
|
||||
if (!easternLocalValue) {
|
||||
return "UTC: not set";
|
||||
}
|
||||
|
||||
const utcInstant = easternLocalToUtcInstant(easternLocalValue);
|
||||
if (!utcInstant || Number.isNaN(utcInstant.getTime())) {
|
||||
return "UTC: invalid ET value";
|
||||
}
|
||||
|
||||
const formatted = new Intl.DateTimeFormat("en-US", {
|
||||
timeZone: "UTC",
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
hour12: false,
|
||||
timeZoneName: "short"
|
||||
}).format(utcInstant);
|
||||
|
||||
return "UTC: " + formatted;
|
||||
}
|
||||
|
||||
function refreshDisplays() {
|
||||
if (startUtcDisplay && startInput) {
|
||||
startUtcDisplay.textContent = toUtcText(startInput.value);
|
||||
}
|
||||
|
||||
if (endUtcDisplay && endInput) {
|
||||
endUtcDisplay.textContent = toUtcText(endInput.value);
|
||||
}
|
||||
}
|
||||
|
||||
if (startInput) {
|
||||
startInput.addEventListener("input", refreshDisplays);
|
||||
startInput.addEventListener("change", refreshDisplays);
|
||||
}
|
||||
|
||||
if (endInput) {
|
||||
endInput.addEventListener("input", refreshDisplays);
|
||||
endInput.addEventListener("change", refreshDisplays);
|
||||
}
|
||||
|
||||
refreshDisplays();
|
||||
})();
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user