有人知道如何将JS日期时间转换为MySQL日期时间吗?此外,是否有一种方法可以向JS日期时间添加特定数量的分钟,然后将其传递给MySQL日期时间?
有人知道如何将JS日期时间转换为MySQL日期时间吗?此外,是否有一种方法可以向JS日期时间添加特定数量的分钟,然后将其传递给MySQL日期时间?
var date;
date = new Date();
date = date.getUTCFullYear() + '-' +
('00' + (date.getUTCMonth()+1)).slice(-2) + '-' +
('00' + date.getUTCDate()).slice(-2) + ' ' +
('00' + date.getUTCHours()).slice(-2) + ':' +
('00' + date.getUTCMinutes()).slice(-2) + ':' +
('00' + date.getUTCSeconds()).slice(-2);
console.log(date);
new Date().toISOString().slice(0, 19).replace('T', ' ');
输出:
2012-06-22 05:40:06
对于更高级的用例,包括控制时区,请考虑使用http://momentjs.com/:
require('moment')().format('YYYY-MM-DD HH:mm:ss');
如果你需要一种轻量级的替代方案momentjs,可以考虑使用https://github.com/taylorhakes/fecha
require('fecha').format('YYYY-MM-DD HH:mm:ss')
我认为可以使用方法toISOString()
来简化解决方案,它具有广泛的浏览器兼容性。
这样你的表达式就只需要一行:
new Date().toISOString().slice(0, 19).replace('T', ' ');
"2017-06-29 17:54:04"
new Date(1091040026000).toISOString().slice(0, 19).replace('T', ' ');
- JohnDate
的时区偏移量之后获取小时、天、月(甚至年)时间。而你的方法返回MySQL DATETIME格式中基础UTC时间。大多数情况下,存储UTC可能更好,无论哪种情况,您的数据表应该在一个字段中提供位置信息。或者,转换为本地时间非常容易:使用...- Date.getTimezoneOffset() * 60 * 1000
(请注意,在适用的情况下还要调整夏令时)。 - mike rodent尽管JavaScript具备足够的基本工具来完成这个任务,但它还是相当笨拙的。
/**
* You first need to create a formatting function to pad numbers to two digits…
**/
function twoDigits(d) {
if(0 <= d && d < 10) return "0" + d.toString();
if(-10 < d && d < 0) return "-0" + (-1*d).toString();
return d.toString();
}
/**
* …and then create the method to output the date string as desired.
* Some people hate using prototypes this way, but if you are going
* to apply this to more than one Date object, having it as a prototype
* makes sense.
**/
Date.prototype.toMysqlFormat = function() {
return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + " " + twoDigits(this.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};
Date
对象。例如:new Date().toMysqlFormat()
或者 new Date(2014,12,14).toMysqlFormat()
等等。 - kojiroconst DATE_FORMATER = require( 'dateformat' );
const datetime = DATE_FORMATER( new Date(), "yyyy-mm-dd HH:MM:ss" );
或者
const MOMENT = require( 'moment' );
const datetime = MOMENT().format( 'YYYY-MM-DD HH:mm:ss.000' );
${d.toLocaleDateString(“EN-CA”} $ {d.toLocaleTimeString(“EN-GB”)}
- havlock// Your default date object
var starttime = new Date();
// Get the iso time (GMT 0 == UTC 0)
var isotime = new Date((new Date(starttime)).toISOString() );
// getTime() is the unix time value, in milliseconds.
// getTimezoneOffset() is UTC time and local time in minutes.
// 60000 = 60*1000 converts getTimezoneOffset() from minutes to milliseconds.
var fixedtime = new Date(isotime.getTime()-(starttime.getTimezoneOffset()*60000));
// toISOString() is always 24 characters long: YYYY-MM-DDTHH:mm:ss.sssZ.
// .slice(0, 19) removes the last 5 chars, ".sssZ",which is (UTC offset).
// .replace('T', ' ') removes the pad between the date and time.
var formatedMysqlString = fixedtime.toISOString().slice(0, 19).replace('T', ' ');
console.log( formatedMysqlString );
或者一个单行解决方案,
var formatedMysqlString = (new Date ((new Date((new Date(new Date())).toISOString() )).getTime() - ((new Date()).getTimezoneOffset()*60000))).toISOString().slice(0, 19).replace('T', ' ');
console.log( formatedMysqlString );
当在mysql中使用时间戳时,此解决方案也适用于Node.js。
new Date().toISOString().slice(0, 10)+" "+new Date().toLocaleTimeString('en-GB');
const toSqlDatetime = (inputDate) => {
const date = new Date(inputDate)
const dateWithOffest = new Date(date.getTime() - (date.getTimezoneOffset() * 60000))
return dateWithOffest
.toISOString()
.slice(0, 19)
.replace('T', ' ')
}
toSqlDatetime(new Date()) // 2019-08-07 11:58:57
toSqlDatetime(new Date('2016-6-23 1:54:16')) // 2016-06-23 01:54:16
请注意,@Paulo Roberto的回答在新的一天开始时会产生错误的结果(我无法留言)。例如:
var d = new Date('2016-6-23 1:54:16'),
finalDate = d.toISOString().split('T')[0]+' '+d.toTimeString().split(' ')[0];
console.log(finalDate); // 2016-06-22 01:54:16
new Date().toISOString().slice(0, 19).replace("T", " ")
new Date().toISOString().replace("T", " ")
-- 你真的需要 (dot)slice 吗? - edwardsmarkf我很惊讶还没有人提到适用于JavaScript的瑞典日期时间格式。
使用语言标记为sv-SE的BCP 47可用于new Date
"locale"参数。
我不是在说这是一个好的做法,但它是有效的。
console.log(new Date().toLocaleString([['sv-SE']])) //2022-09-10 17:02:39
var d = new Date(); d.toISOString().split('T')[0]+' '+d.toTimeString().split(' ')[0];
该代码会返回当前日期和时间,包含时区信息。 - Paulo Roberto Rosa