创建JS Date类的格式为Y-m-d H:i:s。

时间:2021-11-14 21:28:55

I am newbie in JS programming language and i got stuck in a little problem. I have an AjaxRequest with receive variable in JSON format. One of those parameters are a date with the following format Y-m-d H:i:s (eg 2015-02-07 11:52:26), and i want to compare with current date. The issue is that i cannot convert into Date object, and, also i cannot make it on the controller side to have the requested formt Y-m-dTH:i:s. I am wondering if i could do anything to compare those date.

我是JS编程语言的新手,我遇到了一个小问题。我有一个以JSON格式接收变量的AjaxRequest。其中一个参数是以下格式的日期:i:s (eg: 2015-02-07 11:52:26),我想比较一下当前日期。问题是我不能转换到Date对象,而且,我也不能在控制器端使它得到请求的formt -m- dth:i:s。我想知道我是否能做些什么来比较那些日子。

Thank you!

谢谢你们!

1 个解决方案

#1


1  

Do this:

这样做:

var date = "2015-02-07 11:52:26"; //in your code the request data returned as JSON string

dateEdit = date.replace(/-/g, "/");
dateEdit = new Date(dateEdit);
document.write("Using /: " + dateEdit.toString());

dateEdit = date.replace(/\s/g, "T");
dateEdit = new Date(dateEdit);
document.write("<br /><br />Using T: " + dateEdit.toString());

Or replace the space between the date and the time with a capital T to render this as a valid string:

或者用大写的T来替换日期和时间之间的空格,将其呈现为有效字符串:

#1


1  

Do this:

这样做:

var date = "2015-02-07 11:52:26"; //in your code the request data returned as JSON string

dateEdit = date.replace(/-/g, "/");
dateEdit = new Date(dateEdit);
document.write("Using /: " + dateEdit.toString());

dateEdit = date.replace(/\s/g, "T");
dateEdit = new Date(dateEdit);
document.write("<br /><br />Using T: " + dateEdit.toString());

Or replace the space between the date and the time with a capital T to render this as a valid string:

或者用大写的T来替换日期和时间之间的空格,将其呈现为有效字符串: