0

The current piece of code that I am using to do this looks like this,

x3 = new Date(x1.value).format('yyyymmdd');

This works well in Firefox, Chrome and generally everything else, with the exception of the great Internet Explorer.

I Have tried other code such as...

<script>

function parseISO8601(dateStringInRange) {
    var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
    date = new Date(NaN), month,
    parts = isoExp.exec(dateStringInRange);

    if(parts) {
      month = +parts[2];
      date.setFullYear(parts[1], month - 1, parts[3]);
      if(month != date.getMonth() + 1) {
        date.setTime(NaN);
      }
    }

    alert(date);
    return date;

}   

parseISO8601('2013-01-21'); 

</script>

This works in all browsers but gives me an output like this.....

Mon Jan 21 2013 00:00:00 GMT+0000 (GMT Standard Time)

when i really need it to look like this

20130121

has anyone had this problem and figured a way to solve it???

Pratik
  • 30,639
  • 18
  • 84
  • 159
MikeyJ
  • 454
  • 1
  • 5
  • 16
  • when i use the original piece of code in IE and alert the result it looks like this NaNNaNNaN – MikeyJ Jan 16 '13 at 10:29

1 Answers1

1

Use the functions here to build a new string out of the date you return from your function.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date

getDate(), getMonth(), and etc should be what you're looking for

Example:

var toConvert = parseISO8601('2013-01-21');
var newForm = toConvert.getYear().toString()+toConvert.getMonth().toString()+toConvert.getDate().toString();

Then if you need the newForm in integer form, then just run parseInt(newForm);

tkone
  • 22,092
  • 5
  • 54
  • 78
Kevin Wang
  • 3,290
  • 1
  • 28
  • 40
  • You'd need to insert leading zeros for month and day that are less than 10. – nnnnnn Jan 16 '13 at 10:34
  • ^Correct. Don't forget about that too. Regardless the form should be easy enough to build with those functions. – Kevin Wang Jan 16 '13 at 10:35
  • Fixed that link for you. Please don't link to w3schools. Content is frequently out of date and just plain inaccurate. http://w3fools.com – tkone Jan 16 '13 at 10:47
  • Thanks, Ive tried this but nothing seems to be able to find parseISO8601 i tried using new Date() but again IE wont accept it, any reason why it might not be able to use parseISO8601 ? – MikeyJ Jan 16 '13 at 12:53
  • I thought you said parseISO8601 gives you Mon Jan 21 2013 00:00:00 GMT+0000 (GMT Standard Time)? The parse function returns a date object you can change into a numerical format. – Kevin Wang Jan 18 '13 at 03:08