0

Could someone please advise on how I can change the selected date and time into ISO format, for example:

From: 18 August 2022 08:15 PM

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

I am using Simplepicker.

Please find code below:

 <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>
Louis
  • 73
  • 1
  • 8
  • To get a standard ISO formatted date, you could just used the `.toISOString()` function. However, the desired date format you want is a little different, so you will probably have to create the date string yourself using JavaScript [Date Object Methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#instance_methods) – EssXTee Aug 18 '22 at 19:42

1 Answers1

1

The second parameter to the .on('submit', ... callback is a JS date object. Use that instead of readableDate to create the desired format.

// 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
danh
  • 62,181
  • 10
  • 95
  • 136