1

I'm having troubles learning how to pull XML using JavaScript and i'm kinda stuck. I would like to convert a time zone for example: 2018-05-25T23:03:26Z and I would like to convert it to my time zone which is Atlantic. The code below is something I tried to do with a function to convert the example above, but for some reason it won't work. If anyone can point me in the right direction so I can achieve this programming task that would be fantastic. Thanks in advance!

var app = {

    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    onDeviceReady: function() {
        retrieveDataFeed();
    }
};

function retrieveDataFeed(){
    var doc = new XMLHttpRequest();
    doc.onreadystatechange = function(){
        if(this.readyState == 4 && this.status == 200){
            //document.getElementsByClassName('app')[0].innerHTML = this.responseText;
            parseXMLDoc(this.responseXML);
        }
    }
    doc.open("GET", "myXml", false);
    doc.send();
}

function parseXMLDoc(feed){
    var pageTitle = feed.querySelector('feed > title');
    document.getElementById('title').innerHTML = '<h1>' + pageTitle.firstChild.nodeValue + '</h1>';
   // var weatherEntries = feed.getElementsByTagName('entry');
    //console.log(weatherEntries.length);

   // var entryChildren = weatherEntries[1].childNodes;
  //  for(i = 0; i < entryChildren.length; i++){
      //  if(entryChildren[i].nodeName == 'summary'){
         //   document.getElementById('curr').innerHTML = entryChildren[i].firstChild.nodeValue;
       // }
   // }

    // console.log(weatherEntries[1].firstChild.nextSibling.firstChild.nodeValue);      ****         // adding new test shit below this comment

   // var testEntry = feed.getElementsByTagName('title');
   // document.getElementById('curr').innerHTML = testEntry[1].firstChild.nextSibling.firstChild.nodeValue

    var time = feed.getElementsByTagName("updated")[0].childNodes[0].nodeValue; // testing this 

  // var convertedTime = toTimeZone(time);

    document.getElementById("curr").innerHTML =  time;



    //var date = time; 
     //isoDate = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString();


    //var testTime = feed.querySelector('feed > title');


   //document.getElementById('curr'.innerHTML = '<p>' + testTime.firstChild.nodeValue + '</p>');

    //var testEntry = feed.getElementsByTagName('title');

}

function toTimeZone(time, zone) {
    var format = 'YYYY/MM/DD HH:mm:ss ZZ';
    return moment(time, format).tz(zone).format(format);
}

app.initialize();

Update: I was able to do what I wanted but, at the end of my date, I want to remove the timezone: (Atlantic Provincial timezone), How would I do this? Substring won't seem to work. But a substring probably wouldn't work anyway, cause the date is dynamic, which i'd need to just remove the end my string somehow.

var time  = feed.getElementsByTagName("updated")[0].childNodes[0].nodeValue; 



      var date = new Date(time);
      date.toString(); // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)" format




      //var cutText = date.replace("Atlantic Daylight Time", " ");
     // var convertedTime = toTimeZone(time);
    //dateobj = time;


    // while creating Date object
    //date.substring( 0, str.indexOf( "At" ) );   
    //var removeThis = "(Atlantic Daylight Time)"

        document.getElementById("curr").innerHTML =  date.substring( 0, date.indexOf( "At" ) );

1 Answers1

0

It sounds like you have already worked it out.

Your xml dates in the feed are all in UTC format (GMT). (There is no number after the Z to adjust to another zone). The Date command will automatically convert this datetime string to a datetime stored in UTC format.

You can then format this date to your local time zone using the toString() format which reflects the time zone you are in. https://stackoverflow.com/a/5619588/1692270

var weather1s='2018-05-25T23:03:26Z';
var weather1d=new Date(weather1s);

 //UTC & BST in my case, UTC & Atlantic in your case
 console.log('UTC ',weather1d.toUTCString());
 console.log('Local ',weather1d.toString());

If you want more control of the layout or code for multiple languages/regions there is more here. This is new to me, I just came across it for the 1st time today.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl https://caniuse.com/#search=internationalization

JohnC
  • 2,687
  • 1
  • 22
  • 30