Home > Article > Web Front-end > How to convert JavaScript datetime to MySQL datetime?
Datetime operations in JavaScript are very important when working with databases. JavaScript dates and times are different from MySQL dates and times. JavaScript provides a variety of ways to represent dates and times, all of which are different from MySQL's date and time formats. In this article, we will discuss some methods to convert JS date and time to MySQL date and time format.
First, we need to 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 some methods to convert JS date to MySQL date format -
Use String split() and slice() methods
Use String Replace() and slice() methods
Here are the steps followed in this method -
Get a Javascript date and convert it to ISO date format using the .toISOString() method.
Use the String.split() method to split the ISO string into two parts and use "T" as the separator
Declare two variables data and time and assign the corresponding parts of String.
Merge date and time strings.
In this example, we use the split() and slice() methods to convert JavaScript datetime to MySQL datetime.
<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; }
Here are the steps followed in this method -
Get a Javascript date and convert it to ISO date format using the .toISOString() method.
Replace T with spaces.
Slice the ISO date string until the 19th character
In this example, we use the replace() and slice() methods to convert JavaScript datetime to MySQL datetime.
<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; }
Here we have discussed two methods to convert JavaScript datetime to MySQL datetime with examples.
The above is the detailed content of How to convert JavaScript datetime to MySQL datetime?. For more information, please follow other related articles on the PHP Chinese website!