首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Simplepicker将日期转换为ISO格式

使用Simplepicker将日期转换为ISO格式
EN

Stack Overflow用户
提问于 2022-08-18 19:27:16
回答 1查看 33关注 0票数 0

请有人告知我如何将选定的日期和时间更改为ISO格式,例如:

:2022年8月18日08:15

To:2022-08-18 20:15:10

我正在使用简化器

请查找以下代码:

代码语言:javascript
复制
 <script>
            
              let simplepicker = new SimplePicker({
                zIndex: 10,
              });
              
              const $button = document.querySelector('.simplepicker-btn');
              const $eventLog = document.querySelector('.event-log');
              $button.addEventListener('click', (e) => {
                simplepicker.open();
              });
              
              const input = document.getElementById("myInput"); // <- 1) Grab the input
              // $eventLog.innerHTML += '\n\n';
              simplepicker.on('submit', (date, readableDate) => {
              $eventLog.innerHTML += readableDate + '\n';
              input.value = readableDate; // <- 2) Update the input value
                
               
              });
              simplepicker.on('close', (date) => {
                $eventLog.innerHTML += 'Closed' + '\n';
              });
              
            </script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-18 19:53:47

.on('submit', ...回调的第二个参数是JS对象。使用它而不是readableDate来创建所需的格式。

代码语言:javascript
复制
// note that a js date called "date" is the second param to the submit callback
const date = new Date();

// truncate thru position 18 to get mm-dd-yy hh:mm:ss
// and remove the "T" that delimits the time substring
const formatted = date.toISOString().substring(0,19).replace('T', ' ')
console.log(formatted)

// now use "formatted" in the dom
// $eventLog.innerHTML += formatted + '\n';
// etc

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73408475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档