I am retrieving some data from an xml file and wish to split the result, parse it and then print it into an html element.
Here is an example of my xml file:
<Root>
<Foo>
<Bar>
<BarType>Green</BarType>
<Date>2020-09-03 23:40:55</Date>
</Bar>
</Foo>
</Root>
I would like to both split the string from Date and make it more human readable.
so instead of "2020-09-03 23:40:55" it would be "Sept 03, 2020 at 23:40"
Here is my function so far:
function testFoo() {
$.ajax({
type: "GET",
url: "/assets/TestFeed.xml",
dataType: "xml",
// Alert if error in reading xml
error: function (e) {
alert("An error occurred while processing XML file");
console.log("XML reading Failed: ", e);
},
// Runs if xml read succesully
success: function (response) {
$(response).find("Foo").each(function () {
// Finds Date in specific xml node
var fooDate = $(this).find('Bar:has(BarType:contains("Green")) Date').text();
// Test for console to make sure it works
console.log(fooDate);
// add to the HTML
$("p").append('<span class="date-text">' + "Last Updated: " + '</span>' + '<span class="date-first">' + fooDate + '</span>' + '<span class="date-time">' + "at " + fooDate + '</span>');
});
}
});
}
2
Answers
If you can use momentjs:
You can use built-in Date constructor:
or
or