1

I have the the following date format in my jQuery. But when I try to do a dateparse using the datepicker it states that the object is not supported. I take it that the date format is incorrect.

The following line works fine in Chrome and Firefox but not in IE. I get NaN returned as a result:

var newdate = getNewDate(value);

function getNewDate(dateValue) {
    var newDate = new Date(dateValue);
    var cDate  = newDate.getDate();
    var cMonth = newDate.getMonth() + 1; // have to add one as January starts from 0
    var cYear = newDate.getFullYear();

    return cDate + "/" + cMonth + "/" + cYear;
};

When I do getDate it returns NaN for each of the gets, i.e. getDate, getMonth and getFullYear.

All I require is to get the date and return in this format dd/mm/yyyy

Any ideas?

dda
  • 6,030
  • 2
  • 25
  • 34
tjhack
  • 1,022
  • 3
  • 20
  • 43
  • possible duplicate of [Parsing a date in long format from ATOM feed](http://stackoverflow.com/questions/1416296/parsing-a-date-in-long-format-from-atom-feed) – Ja͢ck Jun 10 '12 at 15:21

3 Answers3

1

Try this,

Demo

var value = '2012-04-01T23:00:00Z';
var newdate = getNewDate(value);
alert(newdate);
function getNewDate(dateValue) {
    var newDate = new Date(dateValue.substring(0,dateValue.indexOf('T')).replace(/-/g,'/'));
    var cDate  = newDate.getDate();
    // have to add one as January starts from 0
    var cMonth = newDate.getMonth() + 1; 
    var cYear = newDate.getFullYear(); 
    return cDate + "/" + cMonth + "/" + cYear;
};
Jashwant
  • 28,410
  • 16
  • 70
  • 105
1

You can split amd substring functions

function getNewDate(dateValue) {

var dataAarr = dateValue.split('-'); 


   return dataAarr[2].substring(0,2) + "/" + dataAarr[1] + "/" + dataAarr[0];
};
stay_hungry
  • 1,448
  • 1
  • 14
  • 21
0

Working example here

var d = new Date();

var month = d.getMonth() + 1;
var day = d.getDate();

var output = (('' + day).length < 2 ? '0' : '') + day + '/' + (('' + month).length < 2 ? '0' : '') + month + '/' + d.getFullYear();
just eric
  • 927
  • 1
  • 11
  • 23