[RESOLVED]JavaScript Date madness!!

I need help and cannot figure out what is happening:

if I have a string date as follows:  2014-11-07

and I pass it to the Date constructor function:

var d = new Date(2014-11-07);

and then call

toLocaleDateString() :

d.toLocaleDateString()

I get: 11‎/‎6‎/‎2014 for d.

thanks in advance!

You could use custom format.

http://jsfiddle.net/phZr7/1/

That still gives me the wrong date. The problem isn’t with the formatting.

Hi,

I’m even surprised you are not that far. Which browser is this? I believe some other would do the math which would result in the same value than var d=new Date(1996);

Here, I would do :

var d=new Date(2014,10,7);// january is 0 !

My experience is that the Date constructor is quite messy in what it accepts so try to stick to one or two very basic format.

I tried to do it in jsfiddle.net. But I get the correct result. Is it possible that the server is located in ‘future’ timezone? Try using getUTCDate() and see if you get the correct date.

d1 = new Date("2014-10-7");
alert(d1.toLocaleDateString());

d2 = d1.getUTCDate();
alert(d1.getUTCDate());

I’m assuming that you are trying to use the dateString version of the Date constructor, which if that is true, you’ll need to wrap your parameter in quotes (e.g. new Date(‘yyyy-MM-dd’)). This could be a time zone issue which might be resulting in
your Date being offset by enough to push the day back, but it might be an issue with your leading zero for your Date. I tried using "2014-11-7" and received "11/7/2014" as expected :

var d = new Date('2014-11-7');
alert(d.toLocaleDateString()); // yields "11/7/2014"

You can see an example of this here.

Try adding the TimexoneOffset value to your date value and then try to get the localeDateString

You can try with the below code

//Your Date Variable
      var d = new Date('2014-11-08');
//Variable after adding timezoneoffset value var formatteddate = new Date(d.getTime() + d.getTimezoneOffset()*60000);
//Get the localeDateString alert(formatteddate.toLocaleDateString());

Leave a Reply