Search code examples
javascriptdatetimedate-format

Date formatting through Javascript - JQuery


Dates are always a kick in the nuts (at least for me) and I faced the fact that Javascript doesn't seem to have a method to format dates.

I'm trying to format a date to use the Google API which ask you for a date in this format: yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffff (e.g. 2011-06-25T10:00:00.000+02:00)

I need to be able to read that kind of string and to produce one.


Solution

  • I often use jQuery plugin: http://plugins.jquery.com/project/jquery-dateFormat

    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="https://raw.github.com/phstc/jquery-dateFormat/master/jquery.dateFormat-1.0.js"></script>
    <script type="text/javascript">
    $(function() {
        var d = new Date();
        alert($.format.date(d, 'yyyy-MM-ddTHH:mm:ss'));
    })
    </script>