简体   繁体   English

从Ymd H:i:s格式创建JS Date类

[英]Create JS Date class from format Y-m-d H:i:s

I am newbie in JS programming language and i got stuck in a little problem. 我是JS编程语言的新手,但遇到了一个小问题。 I have an AjaxRequest with receive variable in JSON format. 我有一个带有JSON格式的接收变量的AjaxRequest。 One of those parameters are a date with the following format Ymd H:i:s (eg 2015-02-07 11:52:26), and i want to compare with current date. 这些参数之一是具有以下格式的日期Ymd H:i:s(例如2015-02-07 11:52:26),我想与当前日期进行比较。 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 Ym-dTH:i:s. 问题是我无法转换为Date对象,也无法在控制器端使其具有请求的格式Ym-dTH:i:s。 I am wondering if i could do anything to compare those date. 我想知道我是否可以做些什么来比较那些日期。

Thank you! 谢谢!

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替换日期和时间之间的空格,以将其呈现为有效字符串:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM