
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
- Related Articles
- How to convert MySQL DATETIME value to JSON format in JavaScript?
- How to convert MySQL datetime to Unix timestamp?
- How to convert timestamp to datetime in MySQL?
- Convert INT to DATETIME in MySQL?
- How to convert DateTime to a number in MySQL?
- How to convert JS date time to MySQL datetime?
- How to compare Python DateTime with Javascript DateTime?
- MySQL Query to convert from datetime to date?
- How to convert char field to datetime field in MySQL?
- How to Convert Text Datetime Format to Real Datetime Format in Excel?
- Convert datetime to get month name in MySQL?
- How to convert string to 24-hour datetime format in MySQL?
- How to Convert yyyymmddhhmmss Date Format to Normal Datetime in Excel?Datetime in Excel?
- Convert from varchar to datetime and compare in MySQL?
- How to convert date to datetime in Python?
