我正在尝试使用JavaScript将一个日期对象转换为有效的MySQL日期 - 什么是最好的方法?
我试图使用JavaScript将日期对象转换为有效的MySQL日期 - 最佳方法是什么?我正在尝试使用JavaScript将一个日期对象转换为有效的MySQL日期 - 什么是最好的方法?
我试图使用JavaScript将日期对象转换为有效的MySQL日期 - 最佳方法是什么?const date = new Date().toJSON().slice(0, 10)
console.log(date) //2015-07-23
对于日期时间:
const datetime = new Date().toJSON().slice(0, 19).replace('T', ' ')
console.log(datetime) //2015-07-23 11:26:00
更新:在2021年,Date.js已经多年没有维护,并且不建议使用,Moment.js处于“仅维护”模式。我们可以使用内置的Intl.DateTimeFormat
、Intl.RelativeTimeFormat
和(即将推出的)Temporal
,最好使用这些。一些有用的链接在Moment进入维护模式页面中。
旧版答案:
最好使用像Date.js这样的库(尽管它好几年没有维护了)或者Moment.js。
但如果要手动实现,可以使用Date#getFullYear()
、Date#getMonth()
(月份从0开始,例如0代表一月,因此可能需要+1),以及Date#getDate()
(一个月的哪一天)。只需将月份和日期填充为两个字符,例如:
(function() {
Date.prototype.toYMD = Date_toYMD;
function Date_toYMD() {
var year, month, day;
year = String(this.getFullYear());
month = String(this.getMonth() + 1);
if (month.length == 1) {
month = "0" + month;
}
day = String(this.getDate());
if (day.length == 1) {
day = "0" + day;
}
return year + "-" + month + "-" + day;
}
})();
使用方法:
var dt = new Date();
var str = dt.toYMD();
请注意,该函数有一个名称,这在调试时很有用,但由于匿名作用域函数,不会污染全局命名空间。
该函数使用本地时间;对于UTC,请使用UTC版本(getUTCFullYear
等)。
注意:我只是随口说的,这完全没有经过测试。
Temporal
。 - T.J. CrowderChris Mayhew的最简版回答:
/**
* MySQL date
* @param {Date} [date] Optional date object
* @returns {string}
*/
function mysqlDate(date = new Date()) {
return date.toISOString().split('T')[0];
}
const date = mysqlDate();
console.log(date);
只需编辑字符串即可:
const date = new Date();
const myDate = date.toISOString().replace("T", " ");
const myDateString = myDate.substring(0, myDate.length - 5);
console.log(myDateString);
var
。了解"hoisting"。此外,这个就足够了:new Date().toISOString().slice(0, 10)
。 - GajusDate().toISOString().slice(0, 10)
不够用,例如对于1979年6月7日会出现错误。 - Tin BumtoISOString()
时,您正在将日期从所显示的时区转换为UTC时间。例如,如果您查看的网页位于GMT-6上并显示GMT-6时间,并且您的浏览器运行在GMT-5计算机上,则使用 toISOString()
时,您现在将拥有一个UTC时间,比网页早5个小时而不是6个小时。 - gillyspyfunction js2Sql(cDate) {
return cDate.getFullYear()
+ '-'
+ ("0" + (cDate.getMonth() + 1)).slice(-2)
+ '-'
+ ("0" + cDate.getDate()).slice(-2);
}
const currentDate = new Date();
const sqlDate = js2Sql(currentDate);
console.log(sqlDate);
要将JavaScript日期转换为MySQL日期,您可以执行以下操作:
const currentDate = new Date();
const date = currentDate.toISOString().split("T")[0];
console.log(date);
在第一个例子中有一个小错误,当一天的长度小于1时,它会将月份添加到结果中,而不是天数。
如果你把它改成以下内容,它就可以正常工作了:
if (day.length == 1) {
day = "0" + month;
}
为了
if (day.length == 1) {
day = "0" + day;
}
感谢您发布这个脚本。
修改后的函数如下:
Date.prototype.toYMD = Date_toYMD;
function Date_toYMD() {
var year, month, day;
year = String(this.getFullYear());
month = String(this.getMonth() + 1);
if (month.length == 1) {
month = "0" + month;
}
day = String(this.getDate());
if (day.length == 1) {
day = "0" + day;
}
return year + "-" + month + "-" + day;
}
Object.defineProperties(Date.prototype, {
date: {
get: function() {
return this.toISOString().split('T')[0];
}
},
time: {
get: function() {
return this.toTimeString().match(/\d{2}:\d{2}:\d{2}/)[0];
}
},
datetime: {
get: function() {
return this.date + " " + this.time
}
}
});
const date = (new Date).datetime
console.log(date);
我需要这个文件名,并且需要在当前时区下显示时间。
const timezoneOffset = (new Date()).getTimezoneOffset() * 60000;
const date = (new Date(Date.now() - timezoneOffset))
.toISOString()
.substring(0, 19)
.replace('T', '') // replace T with a space
.replace(/ /g, "_") // replace spaces with an underscore
.replace(/\:/g, "."); // replace colons with a dot
我想说,这可能是最好的方法。刚刚确认它可以工作:
new Date().toISOString().replace('T', ' ').split('Z')[0];