1

I am doing thi:

  var dDate = document.getElementById('<%= date.ClientID %>').value;
              alert(dDate); // 2013/08/14
              dDate = dDate.replace("-", "/");
              dDate = dDate.replace("-", "/");
              var Date = new Date(dDate);  // JAVASCRIPT ERROR HERE...
              alert(Date);

I see simple example but not sure why its not working here?Also it would be helpful if you can tell me how to add +1 Day to date from date.ClientID .

confusedMind
  • 2,573
  • 7
  • 33
  • 74
  • As a sidenote, `new Date('2013/08/14');` should work just fine if you stop using the same name (`Date`) for your variable ? – adeneo Aug 01 '13 at 13:41

5 Answers5

3

The problem is here:

          var Date = new Date(dDate);  // JAVASCRIPT ERROR HERE...

You've hidden the global "Date" constructor with your local variable. Change your variable name to something else and it should work better.

In a JavaScript function, all var declarations from the body of the function "happen" as if they were all at the top. The assignment part of the declaration happens at the point in the code where the declaration is actually found, but the declaration — the addition of the named variable to the function scope, in other words — happens at the top. Thus, when you try to call the "Date" constructor on the right-hand side of the initialization, you're trying to use the value of your "Date" variable, which is undefined at that point.

Also, your .replace() calls are backwards, and you don't need two:

dDate = dDate.replace(/\//g, '-');

Some browsers don't need that (Firefox is OK with the slashes).

edit — oh, and getting a date that's one day after a given date is easy:

var nextDay = new Date( someDate.getTime() );
nextDay.setDate( nextDay.getDate() + 1 );
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Very nice but the next date i get should be in the same format as i am inputting (2013-8-13) as currently it gives this "Sat Aug 3 01:00:00 UTC+0100 2013" – confusedMind Aug 01 '13 at 13:45
  • Oh well you'd have to reformat the date; JavaScript itself doesn't have built-in stuff to do that, but it's not super hard. There are also libraries like Moment.js and DateJS that provide formatting. The "Date" prototype gives you all the "pieces" you need for formatting. – Pointy Aug 01 '13 at 14:14
1

You have your replaces around the wrong way.

 var dDate = document.getElementById('<%= date.ClientID %>').value;
 alert(dDate); // 2013/08/14
 dDate = dDate.replace("-", "/");
 dDate = dDate.replace("-", "/");

If you did another alert here, you'd see it still saying 2013/08/14. Try:

 dDate = dDate.replace("/", "-");

(only one is needed) and retry.

For the second part of your question, see Add days to JavaScript Date:

 var today = new Date();
 var tomorrow = new Date();
 tomorrow.setDate(today.getDate()+1);
Community
  • 1
  • 1
Adrian Wragg
  • 7,311
  • 3
  • 26
  • 50
1

Don't do var Date = new Date() at all. It looks like you may be overriding the default javascript Date() object. Instead, try something like var date = new Date()

Samuel Reid
  • 1,756
  • 12
  • 22
0

Looks like you have to change your replace characters order:

dDate = dDate.replace("/", "-");
dDate = dDate.replace("/", "-");
Maxim Zhukov
  • 10,060
  • 5
  • 44
  • 88
0

the replace should be done like that:

dDate = dDate.replace(/\//g, "-");

Erhard Dinhobl
  • 1,646
  • 18
  • 34