首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将JS日期时间转换为MySQL日期时间

将JS日期时间转换为MySQL日期时间
EN

Stack Overflow用户
提问于 2011-02-27 04:44:53
回答 15查看 258.7K关注 0票数 153

有人知道如何将JS datetime转换为MySQL dateTime吗?另外,有没有一种方法可以将特定的分钟数添加到JS datetime,然后将其传递给MySQL datetime?

EN

回答 15

Stack Overflow用户

回答已采纳

发布于 2011-02-27 22:44:23

虽然JS确实拥有足够的基本工具来完成这项工作,但它相当笨拙。

代码语言: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());
};
票数 95
EN

Stack Overflow用户

发布于 2012-06-22 13:40:51

代码语言:javascript
复制
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);

或者甚至更短:

代码语言:javascript
复制
new Date().toISOString().slice(0, 19).replace('T', ' ');

输出:

代码语言:javascript
复制
2012-06-22 05:40:06

对于更高级的使用情形,包括控制时区,请考虑使用http://momentjs.com/

代码语言:javascript
复制
require('moment')().format('YYYY-MM-DD HH:mm:ss');

对于momentjs的轻量级替代方案,可以考虑使用https://github.com/taylorhakes/fecha

代码语言:javascript
复制
require('fecha').format('YYYY-MM-DD HH:mm:ss')
票数 369
EN

Stack Overflow用户

发布于 2015-05-14 07:43:50

对于任意日期字符串,

代码语言:javascript
复制
// 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 );

或者是单行解决方案,

代码语言:javascript
复制
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。

@Gajus Kuizinas's first answer seems to modify mozilla's toISOString prototype

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5129624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档