将字符串中的日期转换为数据库中插入的对象 [英] Convert date in string to date object for inserting in database

查看:181
本文介绍了将字符串中的日期转换为数据库中插入的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在javascript中将字符串转换为日期对象,以便我可以在DATETIME中将其插入到mysql数据库中。

我尝试新日期(日期)正常工作这里,但在我的js代码中,它没有正常工作。



这里是代码:

  str =Sun Sep 06 2015 11:56:23 GMT + 04:30
new Date(str);

这里是结果:

  2015-09-06T11:56:23.000Z 

此处的结果不同



,并且在数据库中插入时也会收到以下错误。

 日期时间值不正确:'2015-09- 06T11:56:23.000Z'列1'start_date'在行1 

那么如何转换日期?

解决方案

从这里:从JS日期对象以YYYYMMDD格式获取字符串

  Date.prototype.yyyymmdd = function(){
var yyyy = this.getFullYear()。toString();
var mm =(this.getMonth()+ 1).toString(); // getMonth()是基于零的
var dd = this.getDate()。toString();
返回yyyy + - +(mm [1]?mm:0+ mm [0])+ - +(dd [1]?dd:0+ dd [0]) ; // padding
};

然后你可以:

  str =Sun Sep 06 2015 11:56:23 GMT + 04:30
new Date(str).yyyymmdd(); //返回2015-09-06

我们可以对原始功能进行一些修改, 时间



最终JavaScript



  Date.prototype.YYYYMMDDhhmmss = function(){
var YYYY = this.getFullYear()。toString(),
MM =(this.getMonth()+ 1).toString()
DD = this.getDate()。toString(),
hh = this.getUTCHours()。toString(),
mm = this.getUTCMinutes()。toString(),
ss = this.getUTCSeconds()。toString();
返回YYYY + - +(MM [1]?MM:0+ MM [0])+ - +(DD [1]?DD:0+ DD [0]) +(hh [1]→hh:0+ hh [0])+:+(mm [1]?mm:0+ mm [0])+ [1]?ss:0+ ss [0]);
};

然后:

 code> str =Sun Sep 06 2015 11:56:23 GMT + 04:30
new Date(str).YYYYMMDDhhmmss(); //返回2015-09-06 07:26:23

像这样 YYYY-MM-DD hh:mm:ss YYYY-MM-DD 对于 DateTime 数据库中的输入。


I want to convert date in string to date object in javascript so that I could insert it in mysql database as DATETIME.

I try new Date(date) as work fine here, but in my js code it didn't work fine.

here is the code:

str = "Sun Sep 06 2015 11:56:23 GMT+04:30"
new Date(str);

and here is the result :

2015-09-06T11:56:23.000Z 

which is different from result in here

and also I get following error when inserting it in database.

Incorrect datetime value: '2015-09-06T11:56:23.000Z' for column 'start_date' at row 1

So how can I convert date?

解决方案

From here: Get String in YYYYMMDD format from JS date object?

Date.prototype.yyyymmdd = function() {
    var yyyy = this.getFullYear().toString();
    var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
    var dd  = this.getDate().toString();
    return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]); // padding
};

Then you can:

str = "Sun Sep 06 2015 11:56:23 GMT+04:30"
new Date(str).yyyymmdd(); //returns "2015-09-06"

We can make some modifications in the original function to incorporate the time as well:

Final JavaScript

Date.prototype.YYYYMMDDhhmmss = function() {
    var YYYY = this.getFullYear().toString(),
        MM = (this.getMonth()+1).toString(),
        DD  = this.getDate().toString(),
        hh = this.getUTCHours().toString(),
        mm = this.getUTCMinutes().toString(),
        ss = this.getUTCSeconds().toString();
    return YYYY + "-" + (MM[1]?MM:"0"+MM[0]) + "-" + (DD[1]?DD:"0"+DD[0]) + " " + (hh[1]?hh:"0"+hh[0]) + ":" + (mm[1]?mm:"0"+mm[0]) + ":" + (ss[1]?ss:"0"+ss[0]);
};

Then:

str = "Sun Sep 06 2015 11:56:23 GMT+04:30"
new Date(str).YYYYMMDDhhmmss(); //returns "2015-09-06 07:26:23"

Either like this YYYY-MM-DD hh:mm:ss or YYYY-MM-DD is fine for a DateTime input in database.

这篇关于将字符串中的日期转换为数据库中插入的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆