开发者社区> 问答> 正文

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

有谁知道如何将JS dateTime转换为MySQL datetime?还有一种方法可以向JS日期时间添加特定的分钟数,然后将其传递给MySQL日期时间?

展开
收起
保持可爱mmm 2020-05-11 17:35:27 790 0
1 条回答
写回答
取消 提交回答
  • 尽管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()); };来源:stack overflow

    2020-05-11 17:37:28
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript函数 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载

相关镜像