0

I got a snippet of code that gets the date out of a xml file. And i found a snippet that converts the date dd/mm/yyyy. This code works fine in Google Chrome but it does not work fine in Firefox, IE or Edge... In the browsers were the code does not work, the function returns NaN/NaN/NaN. But for example: in Google Chrome there is 12/2/2016 retruned. the format of dateFormXml is yyyy/mm/dd and the output format is dd/mm/yyyy Here is my code:

function dateConverter(dateFromXml){
   function format(x){
      //if the day/month is smaller then 10 add a 0 in front of it (9->09)
      return (s < 10) ? '0' + x : x;
   }
   var d = new Date(dateFormXml),
       convertedDate = [format(d.getDate()), format(d.getMonth() + 1), d.getFullYear()].join('/');

   return convertedDate;
}

Can anyone help me to make this cross browser please? :)

NiekF
  • 151
  • 2
  • 13
  • 2
    Can you explain your code ? Looks like there is something missing - 'dateFromXml' is not being used and 'date' is not defined – R.Costa Jan 21 '16 at 11:22
  • 1
    and also post the sample `dateFromXml ` format being passed to function – NiRUS Jan 21 '16 at 11:24
  • Possible duplicate of [Date constructor returns NaN in IE, but works in Firefox and Chrome](http://stackoverflow.com/questions/2182246/date-constructor-returns-nan-in-ie-but-works-in-firefox-and-chrome) – Patrick Evans Jan 21 '16 at 11:26
  • 'dateFromXml' is the date that i get from a huge xml file with a for loop in another function. 'date' must be 'dateFromXml'. but i did not write my function that gets the date because i dont think my problem is in that function. – NiekF Jan 21 '16 at 11:27
  • Thanks to post a duplicate of another question. – NiekF Jan 21 '16 at 11:28
  • As Nirus said, it would be helpful to see the text returned from the XML file, i.e., the value of dataFromXml. This seems the key to understanding the problem. – Yogi Jan 21 '16 at 11:30
  • The format of dateFromXml is yyyy/mm/dd. in 'convertedDate' I convert the dateFromXml to dd/mm/yyyy – NiekF Jan 21 '16 at 11:31
  • Please [edit] the question to add the new information instead of just posting it in the comments. Thank you. – Anders Jan 21 '16 at 11:47
  • In the `format` function, you name the parameter `x`, but only use a variable `s`. Looks like they should be the same? And in the `dataConverter` function, you never use the parameter `dateFromXml`. – Anders Jan 21 '16 at 11:48
  • I have edited my question – NiekF Jan 21 '16 at 12:57

1 Answers1

0
    function dateConverter(dateFromXml){
        function format(x){
            return (x < 10) ? '0' + x : x;
        }
        var modifiedDate = date.split('-').join('/'),
            d = new Date(modifiedDate),
            convertedDate = [format(d.getDate()), format(d.getMonth()), d.getFullYear()].join('/');

        return convertedDate + time;
    }

Hi guys, I allready solved my own problem, thanks to all who left a comment. Now i will explane my code. What i did in FireFox was: i printed out my result on 'd' and on 'dateFromXml'. I saw that 'd' was an invalid date in firefox and 'dateFromXml' return the date yyyy-mm-dd hh:mm:ss. so i started researching and i found a very helpfull webpage (http://dygraphs.com/date-formats.html) On this webpage you will find all kind of formats for dates. And on this webpage i saw that my date that i got from the XML file was in a invalid format because it is written with '-' instead of '/' so i split the date on '-' and joined it with '/'. thats all... :)

NiekF
  • 151
  • 2
  • 13