[英]javascript convert decimal to date
I have a date as a string: var mydate = "05/05/2011"
when I pass this var to a function like: myfunction(mydate);
我将日期作为字符串: var mydate = "05/05/2011"
当我将此var传递给类似的函数: myfunction(mydate);
I alert the results and get a decimal not the string date: 我提醒结果并得到小数而不是字符串日期:
function myfunction(mydate){
alert(mydate);
}
produces : 产生:
0.0004972650422675286 0.0004972650422675286
how do I get it back to a date? 我如何让它回到约会?
Thats the result of the mathematical expression: 5 / 5 / 2011 = 4.972e-4
, ensure the string is quoted. 这是数学表达式的结果: 5 / 5 / 2011 = 4.972e-4
,确保引用字符串。
var x = 5/5/2011; //performs division
as opposed to
var x = "5/5/2011"; //creates a string
I had this problem as well (I was passing the string from codebehind on a popup to a javascript on the opener). 我也有这个问题(我在弹出窗口中将字符串从代码隐藏传递到开启者的javascript)。 My solution was pretty simple. 我的解决方案非常简单。
in asp.net 在asp.net中
<asp:Label ID="foo" runat="server"/>
in javascript 在javascript中
foo.Text = "5/5/2011";
in codebehind: 在codebehind中:
string runThis = "blahblah"
+ "'"
+ foo.Text
+ "';";
Without the single-quote surrounding the text it would spit out the decimal jibberish (ie the mathematical result of 5 / 5 / 2011) 没有围绕文本的单引号,它会吐出十进制乱码(即2011年5月5日的数学结果)
I'm guessing in your situation you could do 我猜你能做的情况
alert("'" + myDate + "'");
尝试使用以下语法:
var mydate = new Date("05/05/2011");
05 divided by 05 divided by 2011 is 0.0004972650422675286, so my guess is it's thinking that 05/05/2011 is an expression not a string. 05除以05除以2011是0.0004972650422675286,所以我的猜测是认为05/05/2011是一个表达式而不是字符串。 Are you sure there are quotes wrapped around the variable? 你确定变量附近有引号吗?
0.0004972650422675286 is 5 / 5 / 2011
(as in division). 0.0004972650422675286是5 / 5 / 2011
(如分部)。
It is likely you are typing your date without quoting the string, or you are passing the string to eval
when you should be passing it to Date.parse
. 您可能在不引用字符串的情况下键入日期,或者在将字符串传递给Date.parse
时将字符串传递给eval
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.