Thursday, September 4, 2025
HomeLanguagesJavascriptHow to convert JavaScript datetime to MySQL datetime ?

How to convert JavaScript datetime to MySQL datetime ?

Given a date in JavaScript DateTime format and the task is to convert this time into MySQL DateTime format using JavaScript

Approach:

Example 1: In this example, the JavaScript DateTime object is converted into MySQL DateTime (UTC format) by using slice() and replace() method. 

Javascript




function GFG_Fun() {
    let date = new Date();
    console.log("MySQL datetime - " +
        date.toISOString().slice(0, 19).replace('T', ' '));
}
 
GFG_Fun();


Output

MySQL datetime - 2023-06-18 03:27:13

Example 2: This is same as previous example but with a different approach and time is in IST, the JS datetime is converted to MySQL datetime by using slice() and replace() method

Javascript




function GFG_Fun() {
     
    let date = new Date();
     
    console.log("MySQL datetime - " +
            date.toISOString().split('T')[0] + ' '
            + date.toTimeString().split(' ')[0]);
}
 
GFG_Fun();


Output

MySQL datetime - 2023-06-18 03:28:31
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6632 POSTS0 COMMENTS
Nicole Veronica
11800 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS