0

I have this date here 2013/06/10, which comes from the database and is set in a variable called date.

I added one day to this date by doing this..

var endDate = new Date(date);
endDate.setDate(endDate.getDate() + 1);

and now I am trying to change the format to yyyy/MM/dd

var finalEndDate = endDate.toString('yyyy/MM/dd');
alert(finalEndDate);

but this returns

Tues Jun 11 2013 Eastern Standard Time, etc.

How do I fix this?

user2327201
  • 409
  • 3
  • 9
  • 20
  • IF you are using jQuery, consider using this plugin: http://www.datejs.com/ – Learner Jun 11 '13 at 19:17
  • endDate.toString('yyyy, MMMM ,dddd') – Head Jun 11 '13 at 19:18
  • 4
    Duplicate of http://stackoverflow.com/q/1056728/. In short, [`Date`'s `.toString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString) doesn't accept arguments. You'll need to format it manually with `getMonth()`, etc. or find a library to format dates beyond `toString()`, `toLocaleString()`, etc. [Moment.js](http://momentjs.com/) is a common suggestion that's still in active development. – Jonathan Lonowski Jun 11 '13 at 19:18
  • Everything you need to know here: http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript – Head Jun 11 '13 at 19:20

2 Answers2

3

As far as I know, toString does not take any arguments. It's easy to construct your format though.

var finalEndDate = endDate.getFullYear() + '/' + (endDate.getMonth() + 1) + '/' + endDate.getDate();

There are several getter methods for each component of the date object to help you construct nearly any format.

Josh
  • 8,082
  • 5
  • 43
  • 41
2

I strongly encourage you to take a look at Moment.js

var str = moment(date, 'YYYY/MM/DD').add('days', 1).format('yyyy/MM/dd');

Note: moment doesn't know yyyy, what's that supposed to be? See http://momentjs.com/docs/#/displaying/format/ for supported format strings.

Prinzhorn
  • 22,120
  • 7
  • 61
  • 65