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" ) );