0

Using the following code to add an extra day to a Date, however, it returns the value in numbers.

expiryDate = oldDate.setDate(olDate.getDate() + 1);
document.write(expiryDate);

Returns 1396393199000

Then, i tried formatting

expiryDate = oldDate.setDate(olDate.getDate() + 1);
document.write(format(expiryDate, "%2D/%2M/%2Y"));

And I get the following error;

Error while evaluating document Date '1396393199000': invalid character at position 5 ('3') JavaScript: error while evaluating script 'content htmlContent'.

Any advice?

8
  • 2
    What's the format() function? Commented Mar 26, 2014 at 17:20
  • 2
    Also, you have olDate and oldDate, is that a typo? Commented Mar 26, 2014 at 17:20
  • 2
    Try passing oldDate to format() instead of expiryDate. The setDate() method will mutate the original object. Commented Mar 26, 2014 at 17:20
  • 1
    setDate doesn't return a new Date Commented Mar 26, 2014 at 17:21
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Mar 26, 2014 at 17:22

1 Answer 1

1

Your expiryDate isnt valid, when you create dates you need to create a new Date();

Like:

var oldDate = new Date();
var expiryDate = new Date(oldDate.setDate(oldDate.getDate() + 1));

then you can run...

var oldDate = new Date();
var expiryDate = new Date(oldDate.setDate(oldDate.getDate() + 1));
document.write(expiryDate.getDate() + '/' + (expiryDate.getMonth() +1) + '/' + expiryDate. getFullYear());

Or you can use your format function from whatever library you are using...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.