Javascript converts decimal date to date

I have a date as a string: var mydate = "05/05/2011" when I pass this var to a function, for example: myfunction(mydate);

I warn the results and get the decimal string, not the date of the string:

 function myfunction(mydate){ alert(mydate); } 

produces:

+0,0004972650422675286

How to return it to a date?

+4
source share
5 answers

I had this problem too (I was passing a line from the code on the popup window to javascript on the opener). My solution was pretty simple.

in asp.net

 <asp:Label ID="foo" runat="server"/> 

in javascript

 foo.Text = "5/5/2011"; 

in codebehind:

 string runThis = "blahblah" + "'" + foo.Text + "';"; 

Without a single quotation mark surrounding the text, she spat out a decimal fraction (i.e., mathematical result 5/5/2011)

I assume that in your situation you could do

 alert("'" + myDate + "'"); 
+2
source

This is the result of a mathematical expression: 5 / 5 / 2011 = 4.972e-4 , make sure the string is quoted.

 var x = 5/5/2011; //performs division as opposed to var x = "5/5/2011"; //creates a string 
+4
source

Try using this syntax:

 var mydate = new Date("05/05/2011"); 
0
source

05 divided by 05 divided by 2011 is 0.0004972650422675286, so I assume that it considers 05/05/2011 to be an expression, not a string. Are you sure there are quotes wrapped around the variable?

0
source

0.0004972650422675286 5/5/2011 (as in division).

Most likely, you print your date without quoting the string, or pass the string to eval when you must pass it to Date.parse .

0
source

All Articles