Marcus Ferenther
Marcus Ferenther

Reputation: 29

Check different date formats

I have function:

sample(date){
  //operations, for example add one week (7 days) 

  return date;
}

var one = new Date('2012-07-16');
var two = new Date('07/16/2012');

var new = sample(one); // or sample(two)

var day = new.getDate();
var month = new.getMonth();
var year = new.gerYear();

alert(day + month + year);

and now i would like show this date, but how can i check format this date? For example:

alert(sample(one));

should show me date with format 2012-07-23 and if

alert(sample(one));

should show me 07/23/2012

but how can i check format current date? is this possible?

Upvotes: 0

Views: 168

Answers (1)

Alnitak
Alnitak

Reputation: 339816

Date objects don't "remember" the format with which they were created - they're just a wrapper for the standard Javascript "milliseconds since the epoch" time values.

You'll need to either roll your own "date to string" functions, or use one of the popular existing libraries (e.g. Datejs)

Upvotes: 2

Related Questions