Use unix timestamps and convert to/from local in js.

This commit is contained in:
mattbk 2023-09-06 21:21:05 -05:00
parent 90b3137165
commit dab1590608

View File

@ -65,9 +65,27 @@ const char index_html[] PROGMEM = R"rawliteral(
<title>ESP Input Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript">
// Utility from https://webreflection.medium.com/using-the-input-datetime-local-9503e7efdce
Date.prototype.toDatetimeLocal = function toDatetimeLocal() {
var
date = this,
ten = function (i) {
return (i < 10 ? '0' : '') + i;
},
YYYY = date.getFullYear(),
MM = ten(date.getMonth() + 1),
DD = ten(date.getDate()),
HH = ten(date.getHours()),
II = ten(date.getMinutes()),
SS = ten(date.getSeconds())
;
return YYYY + '-' + MM + '-' + DD + 'T' +
HH + ':' + II + ':' + SS;
}
// Submit timestamps as unix seconds
var putDate = function(form) {
form.inputTimeUnix.value = Math.floor(Date.now() / 1000) - new Date().getTimezoneOffset()*60;
form.inputTimeUnix.value = Math.floor(Date.now() / 1000);// - new Date().getTimezoneOffset()*60;
form.inputStartTimeUnix.value = ((Date.parse(js_start_time_unix_entry.value))/1000);
//document.getElementById("js_start_time_unix").value = ((Date.parse(js_start_time_unix_entry.value))/1000);
}
@ -79,13 +97,13 @@ const char index_html[] PROGMEM = R"rawliteral(
// Current start date to string
s = %inputStartTimeUnix%;
current_start = new Date(s * 1000);
//current_start_utc = current_start.toUTCString();
document.getElementById('current-start').innerHTML = current_start.toString();
// Show the local time
local_time_unix = new Date().toString();
document.getElementById('current-start').innerHTML = current_start.toLocaleString();
// Show the local time as a string
local_time_unix = new Date().toLocaleString();//toUTCString();
document.getElementById('local-time-unix').innerHTML = local_time_unix.toString();
// fill in the start time field
document.getElementById('js_start_time_unix_entry').value = current_start.toISOString().slice(0,16);
// fill in the start time field as local time
//document.getElementById('js_start_time_unix_entry').value = current_start.toISOString().slice(0,16);
document.getElementById('js_start_time_unix_entry').value = current_start.toDatetimeLocal();
}
</script></head><body>