0

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?

CODE EXAMPLE

Texan78
  • 687
  • 2
  • 16
  • 42
  • I guess you mean you want to get text inside the xml tag called "expires", right? I think it would help so show a little oF the code you use to parse the XML – Koen Peters May 03 '13 at 23:06
  • "it shows the current date for all my output data naturally" Naturally? What do you mean? – Mark Bertenshaw May 03 '13 at 23:10
  • This var dt = (dateFromString('2013-05-02T11:08:00-6:00')); is hardcoded with this 2013-05-02T11:08:00-6:00 so the output to dtstring is the converted date for all my infoboxes. The data is already parsed from the XML file. I just don't know how to set up the var expires to be converted. – Texan78 May 03 '13 at 23:21
  • All that `dateFromString()` code is irrelevant -- your question is just how to parse XML in Javascript? Did you try searching for "parse xml in javascript", there are lots of hits on SO and elsewhere. – Barmar May 03 '13 at 23:27
  • No, I do not need to know how to parse XML in javascript. That has already been done. I need to know how to take the parsed var from the XML of expires and convert it through the date conversion with the code I listed in my original post. How do I take a variable I have parsed to be imputed in the SimpleDateFormat conversion once it's been parsed. – Texan78 May 03 '13 at 23:39
  • I DO NOT NEED TO PARSE IT... The XML has already been parsed to create the var expires which is in date format ISO 8601. I need to know how to take that var expires and input into SimpleDateFormat code I have listed in my original post. It would help if my posts would quiet being edited from its relevant information. – Texan78 May 03 '13 at 23:55
  • 1
    Have you tried `var dt = dateFromString(expires);`? A lot of people are having trouble understanding what your question is so maybe you could provide the portion of code you need help with. – JLRishe May 04 '13 at 03:03
  • Yes I have tried 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

1 Answers1

0

Here is an example of formatting your date to ISO 8601

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date#Example.3A_ISO_8601_formatted_dates

There are several approaches you can use to format the date/time, below is a blog on such examples.

http://blog.stevenlevithan.com/archives/date-time-format

It's hard to understand what you want to do from your wording but maybe this will help you to get a little further along.

Good luck.

Robert Bolton
  • 667
  • 3
  • 8
  • 22
  • Thanks but I don't need help with formatting it. The code in the original post does that. I need to know how to define a parsed variable with the simepleDateformat which is the code in the original post. It's a little hard to understand when my original posts keep getting edited of relevant information when I spend 30 mins writing a post trying to explain it since I don't know and never used javascript before this week. Then my post gets butchered to something that's not even relevant to what I am needing. – Texan78 May 04 '13 at 04:21
  • To pass in the expires variable just call... var formattedExpiresVariable = dateFromString(yourExpiresVariable); right? – Robert Bolton May 04 '13 at 04:27
  • That is the same as this right? [code]var dt = dateFromString(expires);[/code] If so that is the way it is now and still says undefined and it doesn't pass it through the simpleDateFormat. I can pass the expire var directly to my infoboxes and it outputs the unformatted date. I can pass the raw format in the simpleDateFormat like this [code]var dt = (dateFromString('2013-05-02T11:08:00-6:00')); [/code] and it formats it. As soon as I replace the raw format with the parsed expire var it says undefined. The full code is here http://www.mesquiteweather.net/gmap/googlemap.js – Texan78 May 04 '13 at 04:37
  • The return value of (d) in your dateFromString() function doesn't need to be enclosed in parenthesis. Also, in that function... it looks like you meant to expires.split() not s.split(). I think you're getting closer. – Robert Bolton May 04 '13 at 04:45
  • Also, the code that is running "var dt = dateFromString(expires);" and "var expires = new Date();" appear to be running when the page loads as they're not within a function call. So... expires is going to be set to the current date. Maybe you're hitting a timing issue too? – Robert Bolton May 04 '13 at 04:49
  • That was actually what I was messing with. This was dateFromString(s) that is why it was s.plit() but I changed those both to expires. It still shows undefined though when it is clearly defined. I am not sure what I am missing. – Texan78 May 04 '13 at 04:51
  • Your expires variable right above "function dateFromString(expires)"... try giving that a unique name... it looks like (from a quick search) that your defining var expires in two other spots. So, call it something like "expiresDate". Not sure if that is overwriting the value when you're ready to use it or not. – Robert Bolton May 04 '13 at 04:59
  • var expires = new Date(); was just something I was trying. You may have looked at the code when I added that. I have since removed it as it did nothing for me. I have been working on this for 56 hrs straight and I am not going to sleep until I figure out this one little simple problem. If this was PHP I would have been done last week. – Texan78 May 04 '13 at 05:00