将 querstring 参数转换为 javascript 日期对象失败 - NaN
我正在尝试将查询字符串值转换为 Javascript 日期对象,然后将其转换为 ISO 日期格式。
我有三个按钮 - 一个提取查询字符串并显示值 - 工作正常 其次,使用第一个函数提取查询字符串(有效),然后转换为日期 - 失败 - 得到 NaN 第三,将硬编码字符串直接传递给第二个函数 - 有效。
代码如下所示。如何将查询字符串值转换为 Javascript 日期
<head>
<SCRIPT LANGUAGE="JavaScript" SRC="./date.format.js">
</SCRIPT>
</head>
<script>
function DateTest(dt)
{
var myDate = new Date(dt);
alert(myDate);
var newDate = myDate.format("isoDateTime");
document.write(newDate);
alert(newDate);
}
function QueryDateTest(parm)
{
DateTest(getRequestParameter(parm));
}
function getRequestParameter( name ) {
var strReturn = "";
var strHref = window.location.href;
if ( strHref.indexOf("?") > -1 ){
var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
var aQueryString = strQueryString.split("&");
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
if (aQueryString[iParam].indexOf(name.toLowerCase() + "=") > -1 ){
var aParam = aQueryString[iParam].split("=");
strReturn = aParam[1];
break;
}
}
}
alert(unescape(strReturn));
return(unescape(strReturn.toString()));
}
</script>
<body>
<input type="button" id="hello-world1" value="QueryString" onClick="getRequestParameter('create');" />
<input type="button" id="hello-world2" value="ISODate" onClick="QueryDateTest('create');" />
<input type="button" id="hello-world3" value="HardCoded" onClick="DateTest('11/10/2009');" />
I am trying to convert a querystring value into Javascript date object and then converting it into ISO Date format.
I have three button - one extracts the querystring and displays the value - works properly
Second, uses first function to extract querystring (works) and then converts to Date - fails - get NaN
Third, passes a hardcoded string directly to the second function - works.
Code is shown below. How do I get a querstring value to be converted to Javascript Date
<head>
<SCRIPT LANGUAGE="JavaScript" SRC="./date.format.js">
</SCRIPT>
</head>
<script>
function DateTest(dt)
{
var myDate = new Date(dt);
alert(myDate);
var newDate = myDate.format("isoDateTime");
document.write(newDate);
alert(newDate);
}
function QueryDateTest(parm)
{
DateTest(getRequestParameter(parm));
}
function getRequestParameter( name ) {
var strReturn = "";
var strHref = window.location.href;
if ( strHref.indexOf("?") > -1 ){
var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
var aQueryString = strQueryString.split("&");
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
if (aQueryString[iParam].indexOf(name.toLowerCase() + "=") > -1 ){
var aParam = aQueryString[iParam].split("=");
strReturn = aParam[1];
break;
}
}
}
alert(unescape(strReturn));
return(unescape(strReturn.toString()));
}
</script>
<body>
<input type="button" id="hello-world1" value="QueryString" onClick="getRequestParameter('create');" />
<input type="button" id="hello-world2" value="ISODate" onClick="QueryDateTest('create');" />
<input type="button" id="hello-world3" value="HardCoded" onClick="DateTest('11/10/2009');" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Firefox 3.5 和 IE 7 中工作得很好。我能想到的两件事是,在您的查询字符串中您可能不小心输入了错误的日期。就像 ll/10/2009 (这些就像 llama 中的 l)。那么日期将无法正确解析。其次,我们必须查看 date.format.js 并了解它对 Date 对象做了什么。
Works just fine for me in Firefox 3.5 and IE 7. 2 things I can think of, in your query string you might have accidentally typed in a wrong date. Like ll/10/2009 (those are l's like in llama). Then the date would not parse correctly. Second, we would have to see date.format.js and see what its doing to the Date object.
我有你的解决方案。问题出在您的查询字符串中,您的日期周围有引号,并且您的查询字符串解析器还将这些引号放入日期字符串中。 javascript Date 方法对于传入的内容非常讲究,引号会使其返回 NAN。所以这是您的简单修复方法。在您的代码中,只需确保从日期字符串中删除引号即可:
I have your solution. The problem is in your query string, your date has quotes around it, and your query string parser also puts those quotes in the date string. The javascript Date method is very particular about what you pass in, and quotes will make it return a NAN. So here is your easy fix. In your code just make sure you remove the quotes from your date string: