在浏览器输入中显示本地时间,使用 ISO 格式进行 FormData 提交。

huangapple go评论59阅读模式
英文:

Display Local Time in Browser Input, FormData Submit with ISO Format

问题

以下是翻译好的内容:

"I would like to build a form that displays the current local time by default, and submits a timezone aware time to the server on form submit. I can intercept the form data (as shown in my example) and convert the local time to an ISO time, but I would prefer to leave the Javascript out of the form submission (to keep complexity low) and just have the HTML form submit the ISO time.

Is there any standard HTML component that displays a local time, but has a timezone aware value without additional Javascript? To me, it feels like this should be the standard/ expected behavior of <input type="datetime-local"> but it is not.

I understand I am only passing in HH:MM as the value to the <input>, perhaps I can pass in the entire ISO string, and then choose to display only the HH:MM with CSS? I haven't found a method to do so yet."

我想创建一个表单,该表单默认显示当前本地时间,并在提交表单时向服务器提交具有时区感知的时间。我可以拦截表单数据(如我的示例中所示)并将本地时间转换为ISO时间,但我更希望在表单提交时不涉及JavaScript(以保持复杂性较低),只需使用HTML表单提交ISO时间。

是否有任何标准的HTML组件可以显示本地时间,但具有无需额外JavaScript的时区感知值?对我来说,这应该是`<input type="datetime-local">`的标准/预期行为,但事实并非如此。

我明白我只传递了HH:MM作为<input>的值,也许我可以传递整个ISO字符串,然后选择使用CSS仅显示HH:MM?但我还没有找到这样做的方法。

(注意:这是您要求的内容的翻译,不包括代码部分。)

英文:

I would like to build a form that displays the current local time by default, and submits a timezone aware time to the server on form submit. I can intercept the form data (as shown in my example) and convert the local time to an ISO time, but I would prefer to leave the Javascript out of the form submission (to keep complexity low) and just have the HTML form submit the ISO time.

Is there any standard HTML component that displays a local time, but has a timezone aware value without additional Javascript? To me, it feels like this should be the standard/ expected behavior of &lt;input type=&quot;datetime-local&quot;&gt; but it is not.

I understand I am only passing in HH:MM as the value to the &lt;input&gt;, perhaps I can pass in the entire ISO string, and then choose to display only the HH:MM with CSS? I haven't found a method to do so yet.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var now = new Date(Date.now()),
  nowHHMM = now.toTimeString().slice(0, 5),
  inputTime = document.querySelector(&#39;#time&#39;),
  submitButton = document.querySelector(&#39;#submit&#39;),
  form = document.querySelector(&#39;form&#39;);

inputTime.value = nowHHMM;
submitButton.addEventListener(&#39;click&#39;, e =&gt; {
  e.preventDefault(); // To keep form from actually submitting ...
  var formData = new FormData(form);
  for (const [key, value] of formData) {
    console.log(`${key}: ${value}`);
  }
})

<!-- language: lang-css -->

form {
  max-width: 250px;
  display: grid;
  grid-template-columns: 1fr 1fr;
}

form&gt;* {
  margin: 10px;
}

<!-- language: lang-html -->

&lt;form action=&quot;/save/&quot; id=&quot;timeForm&quot; method=&quot;post&quot;&gt;
  &lt;label for=&quot;time&quot;&gt;Local Time: &lt;/label&gt;
  &lt;input id=&quot;time&quot; type=&quot;time&quot; name=&quot;time&quot;&gt;

  &lt;label for=&quot;submit&quot;&gt;Save Time&lt;/label&gt;
  &lt;input id=&quot;submit&quot; type=&quot;submit&quot;&gt;
&lt;/form&gt;

<!-- end snippet -->

答案1

得分: 0

基于@MatthewHerbst对我的问题的评论,我编写了下面的代码。仍然有JavaScript,但它不是在表单提交时发生,而是在页面加载时发生。这感觉有点巧妙,所以我会等待接受答案,看看是否有更好的想法,但目前它将与一些服务器端逻辑一起工作以实现我想要的功能。似乎应该有更好的方法来处理2023年的时区感知日期时间输入,但也许这就是时尚。

还要注意,对于那些打算使用这段代码的人,如果用户在不刷新页面的情况下更改时区(不太可能,并且不适用于我的用例。也许在飞行时使用应用程序时会发生这种情况?),那么时区将被固定为页面加载时的时区。这是一个边缘情况,但值得考虑。

var now = new Date(),
  nowHHMM = now.toTimeString().slice(0, 5),
  inputTime = document.querySelector('#time'),
  submitButton = document.querySelector('#submit'),
  timezoneInput = document.querySelector('#timezone'),
  form = document.querySelector('form');

timezoneInput.value = now.getTimezoneOffset() / 60;
inputTime.value = nowHHMM;

submitButton.addEventListener('click', e => {
  e.preventDefault(); // 防止表单实际提交...
  var formData = new FormData(form);
  for (const [key, value] of formData) {
    console.log(`${key}: ${value}`);
  }
})
form {
  max-width: 250px;
  display: grid;
  grid-template-columns: 1fr 1fr;
}

form > * {
  margin: 10px;
}

#timezone {
  display: none;
}
<form action="/save/" id="timeForm" method="post">
  <label for="time">Local Time: </label>
  <input id="time" type="time" name="time">

  <input id="timezone" name="hoursBehindGMT">

  <label for="submit">Save Time</label>
  <input id="submit" type="submit">
</form>

这是您提供的代码的翻译部分。

英文:

Based on @MatthewHerbst comment on my question, I came up with the code below. There is still JS, but it happens not at form submit time, it happens at page load. This feels hacky, so I'm going to wait to accept the answer to see if anyone has any better ideas, but for now it will work with some server side logic to do what I want. It seems like there should be a better way to handle timezone aware datetime inputs in 2023, but perhaps this is the zeitgeist.

Also note, for anyone who is looking to use this code, if the user changes timezones without refreshing the page (unlikely, and not applicable to my use case. Maybe would happen if using an app while flying?) then the timezone will be stuck what it was when the page loaded. It's an edge case, but good to think about.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var now = new Date(),
  nowHHMM = now.toTimeString().slice(0, 5),
  inputTime = document.querySelector(&#39;#time&#39;),
  submitButton = document.querySelector(&#39;#submit&#39;),
  timezoneInput = document.querySelector(&#39;#timezone&#39;),
  form = document.querySelector(&#39;form&#39;);

timezoneInput.value = now.getTimezoneOffset()/60;
inputTime.value = nowHHMM;

submitButton.addEventListener(&#39;click&#39;, e =&gt; {
  e.preventDefault(); // To keep form from actually submitting ...
  var formData = new FormData(form);
  for (const [key, value] of formData) {
    console.log(`${key}: ${value}`);
  }
})

<!-- language: lang-css -->

form {
  max-width: 250px;
  display: grid;
  grid-template-columns: 1fr 1fr;
}

form&gt;* {
  margin: 10px;
}

#timezone {
  display: none;
}

<!-- language: lang-html -->

&lt;form action=&quot;/save/&quot; id=&quot;timeForm&quot; method=&quot;post&quot;&gt;
  &lt;label for=&quot;time&quot;&gt;Local Time: &lt;/label&gt;
  &lt;input id=&quot;time&quot; type=&quot;time&quot; name=&quot;time&quot;&gt;

  &lt;input id=&quot;timezone&quot; name=&quot;hoursBehindGMT&quot;&gt;

  &lt;label for=&quot;submit&quot;&gt;Save Time&lt;/label&gt;
  &lt;input id=&quot;submit&quot; type=&quot;submit&quot;&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 07:12:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定