将JSdate时间转换为MySQLdate时间

有谁知道如何将JSdate时间转换为MySQLdate时间? 也有一种方法来添加一个特定的分钟数的JSdate时间,然后将其传递给MySQLdate时间?

虽然JS拥有足够的基本工具来做到这一点,但它非常笨重。

/** * 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()); }; 
 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()可以减less笨重的方法,它具有很宽的浏览器兼容性。

所以你的expression将是一个单线的:

 new Date().toISOString().slice(0, 19).replace('T', ' '); 

生成的输出:

“2017-06-29 17:54:04”

历史悠久的DateJS库有一个格式化例程(它覆盖“.toString()”)。 你也可以很容易地做一个,因为“date”方法给你所有你需要的数字。

对于任意datestring,

 // 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 ); 

或者单线解决scheme,

 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 ); 

这个解决scheme也适用于在MySQL中使用Timestamp的Node.js。

@Gajus Kuizinas的第一个答案似乎修改了mozilla的toISOString原型

我给出了简单的JavaScriptdate格式示例,请查看下面的代码

 var data = new Date($.now()); // without jquery remove this $.now() console.log(data)// Thu Jun 23 2016 15:48:24 GMT+0530 (IST) var d = new Date, dformat = [d.getFullYear() ,d.getMonth()+1, d.getDate() ].join('-')+' '+ [d.getHours(), d.getMinutes(), d.getSeconds()].join(':'); console.log(dformat) //2016-6-23 15:54:16 

使用momentjs

 var date = moment().format('YYYY-MM-DD H:mm:ss'); console.log(date) // 2016-06-23 15:59:08 

例如请查看https://jsfiddle.net/sjy3vjwm/2/