AJAX Getting "undefined" when trying to use getMonth on date acquired via AJaX

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Getting "undefined" when trying to use getMonth on date acquired via AJaX

Hello, everyone.

I'm trying to implement a script that will display both the user's local time and the time in St. Louis (based off of the server clock; I want to convert it to UTC, then subtract the hours based upon DST, either -5 or -6.) But I'm hitting a slight snag.

As far as the local time, that's a two-inch putt. I'm grabbing the datetime from the user's computer, and manually formatting it to look like:Your Time: Friday, December 16, 2011 - 08:35:00 PM

I'm using AJaX to get the time from the server using return xmlHttp.getResponseHeader("Date"); and the hours, minutes, and seconds are fine, as is the year. But when I try to use getMonth() and getDay(), the values are coming back as "undefined". I'm assuming because they are expecting integers, but receiving text.

So, how can I retrieve the month and day-of-week from my AJaX?

Thanks,

|||||o . Q
_—__ "You live and you learn; or you don't live long." - Lazarus Long

Simpler still.... in the web page have php write in a date string that is immediately evaluated in to a date object

Code:

serverTimeObject = new Date("<?= date("r");?>");

Then a routine added to keep track of the times. If you search the forums here, you will find examples of such script and please DONT USE setTimeout if your using a timer that repeats at a regular interval, you use setInterval which is only set once and will repeat at the specified timer interval.

If your looking to get the date and time, return a time string which will already be formatted with the day of week name, date, month name, year, time and any offset, all you do is strip that information out of the string.

We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...

So there's no way to get the month and day from the datestring acquired via AJaX?

Please don't take this the wrong way. I appreciate your suggestion. But you assume that I'm using a server-side language like PHP.

Granted, I _am_ using a server-side language, but your solution automatically assumed that I did. My question specifically targeted an AJaX methodology. If I were searching for a server-side solution, I would have posted my question in the appropriate forum.

Is there a way to get the month and day from the datestring being acquired via AJaX?

Thanks,

Last edited by WolfShade; 12-17-2011 at 03:19 PM.

|||||o . Q
_—__ "You live and you learn; or you don't live long." - Lazarus Long

You have ajax request a get from a server-side.php script that returns the server time as a string, that string is bes as an RFC type because the javascript Date() object understands RFC date formats. From that you can grab the month, hour or whatever you want.

My other suggestion was suggested because its the simplest method, get the date and time set by the server in the page delivery and have the client side keep track. It also saves on transactions meaning less of a server load.

We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...