How to convert JavaScript datetime to MySQL datetime?


Date time manipulation in JavaScript is important while dealing with databases. JavaScript Date and time are different from the MySQL date and time. JavaScript provides multiple ways to represent Date and time none of these formats are the same as MySQL's date and time format. In this article, we will discuss some approaches to converting JS date and time into MySQL date and time format.

First of all, we will understand the difference between Javascript and MySQL date and time formats. Here is an example −

Javascript

ISO 8601 Date Format : YYYY-MM-DDTHH:mm:ss.sssZ 

MySQL

ISO 8601 Date Format: YYYY-MM-DD HH:MM:SS 

Here are a few approaches to converting JS Date to MySQL date format −

  • Using String split() and slice() Methods

  • Using String replace() and slice() Methods

Using String split() and slice() Methods

Here are the steps followed in this approach −

  • Get the Javascript Date and convert that into ISO Date format using the .toISOString() method.

  • Split the ISO String into two parts using the String.split( ) method with the separator using “T”

  • Declare two variables data and time and assign the respective parts of the String.

  • Combine the date and time strings.

Example

In this example, we convert JavaScript datetime to MySQL datetime using split() and slice() methods.

<html>
<body>
   <h2>Convert JavaScript datetime to MySQL datetime</h2>
   <p>Click the following button to convert JavaScript datetime to MySQL datetime</p><br>
   <button id="btn" onclick="convert()"> Click Here </button>
   <br>
   <p id="result1">JavaScript Time: </p>
   <p id="result2">MySQL Time: </p>
   <script>
   
      // function to convert JavaScript date to MySQL date-time format
      function convert() {
         let out1 = document.getElementById("result1");
         
         // create a new Date object
         let dt = new Date();
         
         // convert the date object to ISO string format
         dt = dt.toISOString();
         out1.innerText += dt;
         
         // split the ISO string into date and time
         dt = dt.split("T");
         
         // separate the date and time into separate variables
         let date = dt[0];
         let time = dt[1].slice(0, 8);
         
         // combine date and time into a single MySQL-format string
         let mysqlTime = date + " " + time;
         
         // get the output element and set its text content to the MySQL time string
         let out2 = document.getElementById("result2");
         out2.innerText += mysqlTime;
      }
   </script>
</body>
</html>

After some minification the javascript code can be written as −

function convert() {
   let dt = new Date().toISOString().split("T");
   let mysqlTime = dt[0] + " " + dt[1].slice(0, 8);
   let out = document.getElementById("output");
   out.innerText += mysqlTime;
}

Using String replace() and slice() Methods

Here are the steps followed in this approach −

  • Get the Javascript Date and convert that into ISO Date format using the .toISOString() method.

  • Replace the T with a blank Space.

  • Slice the ISO date string till the 19th character

Example

In this example, we are converting JavaScript datetime to MySQL datetime using replace() and slice() methods.

<html>
<body>
   <h2>Convert JavaScript datetime to MySQL datetime</h2> 
   <p>Click the following button to convert JavaScript datetime to MySQL datetime</p><br>
   <button id="btn" onclick="convert( )"> Click Here </button><br>
   <p id="result1">JavaScript Time: </p>
   <p id="result2">MySQL Time: </p>
   <script>
      
      // function to convert JavaScript date to MySQL date-time format
      function convert() {
         let out1 = document.getElementById("result1");
         
         // Create a new Date object
         let dt = new Date();
         
         // Convert the date object to an ISO string
         dt = dt.toISOString();
         out1.innerText += dt;
         
         // Replace the 'T' character with a space
         dt = dt.replace("T", " ")
         
         // Slice the string, up to the 19th character
         dt = dt.slice(0, 19);
         
         // Print the string
         let out2 = document.getElementById("result2");
         out2.innerText += dt;
      }
   </script>
</body>
</html> 

After some minification the javascript code can be written as −

function convert() {
   let dt = new Date().toISOString().replace("T", " ").slice(0, 19);
   let out = document.getElementById("output");
   out.innerText += dt;
}

We have discussed here two approaches to convert JavaScript datetime to MySQL datetime with help of examples.

Updated on: 21-Feb-2023

905 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements