Loading ...
Sorry, an error occurred while loading the content.
 

ISO script

Expand Messages
  • John Hynes
    I write a little JavaScript function to print the combined format ISO 8601 calendar date. You can plug the following function into your JavaScript code, for
    Message 1 of 1 , Jul 15, 2006
      I write a little JavaScript function to print the combined format ISO
      8601 calendar date. You can plug the following function into your
      JavaScript code, for example, document.write(ISO(new Date())) will
      print the current date and time, or you can use any recognized
      date/time string, such as ISO("Saturday, July 15, 2006 9:24:59 PM").
      Since it uses the built-in JavaScript date/time methods, it's pretty
      flexible, although it will not recognize an ISO string. This function
      returns UTC, but it can be easily modified for other time zones, as
      well as week dates and ordinal dates. I have another function that
      converts ISO calendar dates to a recognized date/time string.

      function ISO(DateStr) {
      // convert string to date object
      var DateObj = new Date(DateStr);

      // extract elements of date object
      var Year = DateObj.getUTCFullYear();
      var Month = DateObj.getUTCMonth() + 1; // months range 0-11
      var DOM = DateObj.getUTCDate(); // day of the month
      var Hours = DateObj.getUTCHours();
      var Minutes = DateObj.getUTCMinutes();
      var Seconds = DateObj.getUTCSeconds();

      // ISO requires leading zeros
      if (Month < 10) Month = '0' + Month;
      if (DOM < 10) DOM = '0' + DOM;
      if (Hours < 10) Hours = '0' + Hours;
      if (Minutes < 10) Minutes = '0' + Minutes;
      if (Seconds < 10) Seconds = '0' + Seconds;

      // put all the elements together
      return Year+"-"+Month+"-"+DOM+"T"+Hours+":"+Minutes+":"+Seconds+"Z";
      }

      John Hynes
      2006-07-16T04:46:07Z
    Your message has been successfully submitted and would be delivered to recipients shortly.