I am parsing an XML file for data to display in my Google maps. That all works great. I have one element from the XML I have parsed called "expires", which is the date from XML file in ISO 8601. I have this code to format the date which works great when I enter a raw static ISO 8601 formatted date and output it to my infoboxes via dstring.
This works great for formatting, but shows expires is not defined error.
function dateFromString(expires) {
var bits = expires.split(/[-T:+]/g);
var d = new Date(bits[0], bits[1]-1, bits[2]);
d.setHours(bits[3], bits[4], bits[5]);
// Get supplied time zone offset in minutes
var offsetMinutes = bits[6] * 60 + Number(bits[7]);
var sign = /\d\d-\d\d:\d\d$/.test(expires)? '-' : '+';
// Apply the sign
offsetMinutes = 0 + (sign == '-'? -1 * offsetMinutes : offsetMinutes);
// Apply offset and local timezone
d.setMinutes(d.getMinutes() - offsetMinutes - d.getTimezoneOffset())
// d is now a local time equivalent to the supplied time
return d;
}
var days = ["Sun","Mon","Tues","Wed","Thur","Fri","Sat"];
var months =['Jan','Feb','March','April','May','June','July','Aug','Sept','Oct','Nov','Dec'];
var ampm = " am";
var dt = (dateFromString(expires));
var yr = dt.getFullYear();
var mth = dt.getMonth(); // months in Javascript are 0-11 so May is month 4
mth = months[mth];
var dte = dt.getDate();
var dy = dt.getDay(); // days are 0-6
dy = days[dy];
var hrs = dt.getHours();
var h1 = hrs;
var mins = dt.getMinutes();
if (hrs >= 12) {ampm = " pm"}
if (hrs >= 13) {hrs = hrs - 12}
if (h1 == 0) {hrs = 12}
if (hrs <10) {hrs = "0" + hrs}; // if leading zero desired
if (mins <10) {mins = "0" + mins};
var dtstring = dy + ", " + mth + " " + dte + ", " + yr + " at " + hrs + ":" + mins + ampm;
Now when I manually input and ISO 8601 format like so...
var dt = (dateFromString('2013-05-02T11:08:00-6:00'));
It formats it correctly with the output var dtstring, so I know it works. But it shows the current date for all my output data naturally so I have manually inputted a datestamp instead of defining the var from the parsed XML of expires. I need it to format the expires variable I parsed from the XML file.
Here is where I am stuck...
I am not fluent in Javascript and I have gotten this far with research, When I go to define expires with the code above I am getting a expires not define but it clearly appears to be defined. I can define it directly to my infoboxes and it's fine but not when I define it to be formatted.
What am I missing or doing wrong?
var dt = dateFromString(expires);
and it says it is not defined. That is what I am having trouble with. I have already parsed the XML file. I don't need help with that. I need help with how to define the var expire with the simepleDateFormat which is the code I am using in the original post. I don't need info on how to convert it has that is what the code in the original post does. I am just stuck on how to take that parsed var expires and defining it with that. The URL to the complete code is in the original post. – Texan78 May 04 '13 at 04:08