0

Possible Duplicate:
Formatting a date in javascript

I am new to Javascript / JQuery...and right now all I am trying to do (to learn) is to make a page that extracts information from a google calendar (public) and displays the events along with start date + time plus end date and time. So far I have:

<script type="text/javascript">
$(document).ready(function () {
    var baseURL = "https://www.googleapis.com/calendar/v3/calendars/qq3hkhn3dj8gt05q539smmk2go@group.calendar.google.com/events?key=AIzaSyB66zMow3CSeTdM1m_X_0Wj1JxCtSTd8kU&alt=json&callback=?";
    $.ajax({
        url: baseURL, 
        dataType: 'json',
        success: function(result) {
            console.log(result.items[0].created);
        }         
    });
});
</script>

In my console, the output I see is:

2012-01-07T23:46:15.000Z

Now, what I want to do is, parse this date and time into a pretty looking string, something like "January 1, 2012" and same for the time. How should I proceed? I do not want to step outside JS / JQuery / AJAX.

Also, I would appreciate comments / feedback on my code so far - does this seem a normal / good way to do it, or should I approach it differently?

Thanks, let me know please!

Community
  • 1
  • 1
UserXYZ
  • 223
  • 3
  • 13
  • 2
    Just googling date formats in javascript. https://www.google.com/search?ix=hcb&sourceid=chrome&ie=UTF-8&q=javascript+format+date If you want your code reviewed try - http://codereview.stackexchange.com/ – mrtsherman Jan 08 '12 at 05:33
  • Thanks, but the javascript date object does not include TIME does it? How do I extract that from there? – UserXYZ Jan 08 '12 at 05:42
  • the very first link shows you how to extract time under the h1 heading `get the javascript time` – mrtsherman Jan 08 '12 at 05:43
  • Thanks, but I dont see the format I specified? And how exactly would I use that function? – UserXYZ Jan 08 '12 at 06:09
  • 1
    For the love of Pete.. did you even bother reading the article or trying it. `var foo = new Date('yourstring'); alert(foo.getHours());` – mrtsherman Jan 08 '12 at 06:25

2 Answers2

3

You could try something like this:

var date = new Date(result.items[0].created);
date.toLocaleString();

The format of toLocaleString() is different by browser. If you want precise control over the format, try using a library such as Datejs, XDate, or the formatDate function provided by jQuery UI.

Michael Irwin
  • 3,119
  • 5
  • 24
  • 40
2

You need to parse date string and convert it to date. This date library in Javascript contains some useful functions for the conversion you need.

http://www.mattkruse.com/javascript/date/source.html

I think you can use getDateFromFormat function in this.

Diode
  • 24,570
  • 8
  • 40
  • 51