Skip to content

Commit

Permalink
Locations/entries/date - now takes user time zone;
Browse files Browse the repository at this point in the history
Resolved problem of displayed value, unix epochs and formatting for the input of type date.
  • Loading branch information
cityremade committed Nov 10, 2023
1 parent ae83ebd commit 3c10c8d
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/ui/locations/entries/date.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
export default entry => {

let val
let val;

let d = new Date(entry.value * 1000)
.toLocaleDateString(entry.locale, entry.options); // day only as configured in workspace

let t = new Date(entry.value * 1000)
.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'}); // time only as 00:00

if (entry.edit) {

// these formats are only needed to pass values formatted for the input element
const formats = {
datetime: `${new Date(entry.value * 1000).toLocaleDateString("fr-CA")}T${t}`, // YYYY-MM-DDT00:00
date: `${new Date(entry.value * 1000).toLocaleDateString("fr-CA")}` // YYYY-MM-DD
}

val = mapp.utils.html.node`
<input
type=${entry.type === 'datetime' && 'datetime-local' || 'date'}
value=${entry.value &&
(entry.type === 'datetime' && new Date(entry.value * 1000).toISOString().split('Z')[0]
|| new Date(entry.value * 1000).toISOString().split('T')[0])}
type=${entry.type === 'datetime' ? 'datetime-local' : 'date'}
value="${formats[entry.type]}"
onchange=${e => {
entry.newValue = new Date(e.target.value).getTime() / 1000
// this gets user timezone
//let timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
let unix = new Date(e.target.value).getTime() / 1000;
entry.newValue = unix;
entry.location.view?.dispatchEvent(
new CustomEvent('valChange', {
Expand All @@ -22,9 +37,10 @@ export default entry => {
}}>`;

} else {
}
else {

val = entry.value && new Date(entry.value * 1000).toLocaleString(entry.locale, entry.options)
val = entry.value && entry.type === 'datetime' ? `${d} ${t}` : `${d}`;
}

const node = mapp.utils.html.node`
Expand Down

0 comments on commit 3c10c8d

Please sign in to comment.