如何将 JavaScript 日期时间转换为 MySQL 日期时间?
在处理数据库时,JavaScript 中的日期时间操作非常重要。JavaScript 的日期和时间与 MySQL 的日期和时间不同。JavaScript 提供了多种表示日期和时间的方式,但这些格式均与 MySQL 的日期和时间格式不同。在本文中,我们将讨论一些将 JS 日期和时间转换为 MySQL 日期和时间格式的方法。
首先,我们将了解 Javascript 和 MySQL 日期时间格式之间的区别。以下是一个示例:
Javascript −
ISO 8601 Date Format : YYYY-MM-DDTHH:mm:ss.sssZ
MySQL −
ISO 8601 Date Format: YYYY-MM-DD HH:MM:SS
以下是一些将 JS 日期转换为 MySQL 日期格式的方法:
使用 String split() 和 slice() 方法
使用 String replace() 和 slice() 方法
使用 String split() 和 slice() 方法
此方法遵循以下步骤:
获取 Javascript 日期并使用 .toISOString() 方法将其转换为 ISO 日期格式。
使用 String.split( ) 方法和分隔符“T”将 ISO 字符串拆分为两个部分。
声明两个变量 data 和 time 并为其分配字符串的相应部分。
组合日期和时间字符串。
示例
在此示例中,我们使用 split() 和 slice() 方法将 JavaScript 日期时间转换为 MySQL 日期时间。
<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>
经过一些压缩后,JavaScript 代码可以写成:
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;
}
使用 String replace() 和 slice() 方法
此方法遵循以下步骤:
获取 Javascript 日期并使用 .toISOString() 方法将其转换为 ISO 日期格式。
将 T 替换为空格。
截取 ISO 日期字符串的前 19 个字符。
示例
在此示例中,我们使用 replace() 和 slice() 方法将 JavaScript 日期时间转换为 MySQL 日期时间。
<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>
经过一些压缩后,JavaScript 代码可以写成:
function convert() {
let dt = new Date().toISOString().replace("T", " ").slice(0, 19);
let out = document.getElementById("output");
out.innerText += dt;
}
我们在这里讨论了两种使用示例将 JavaScript 日期时间转换为 MySQL 日期时间的方法。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP