有人知道如何将JS dateTime转换为MySQL dateTime吗?还有没有办法向JS datetime添加特定的分钟数,然后将其传递给MySQL datetime?

推荐答案

虽然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());
};

Mysql相关问答推荐

我可以在插件中访问SQL密钥对吗?

在MySQL中查找具有公共值的列名

在联合查询中使用GROUP BY和ORDER BY

当日期附加到MySQL后,如何按日期说明排序?

如何为Oracle DB查询获得所需的GROUP BY结果?

递归查询意外地为一个字段返回空值

Golang中使用Gorm的Where条件转义字符无法正常工作的问题

发生的原因及解决方法

看不懂mysql自左连接查询

关于设置为 NOT NULL 时的 CHAR 默认值

Next-key lock explication - 范围的主键

在分组结果 MySQL 5.7 版中获取最多的重复值

MySQL IF() 函数根据指定条件执行代码块

Mysql insert with loop out of select 语句

如何查询打印已售罄的产品? [MYSQL]

MySQL 工作台与 phpMyAdmin

由于在 MySQL 中使用保留字作为表名或列名导致的语法错误

MySQL:切换 int 字段值的简单方法

应该使用什么列类型将序列化数据存储在 mysql 数据库中?

SELECT INTO 和未声明的变量错误