pastebin

Paste Search Dynamic
Recent pastes
ISOdatetime
  1. /**
  2.  * ISO 8601:2000 Date Object Extensions
  3.  * @author Lachlan Hunt
  4.  * @version 1.0
  5.  *
  6.  * This script is licenced under a creative commons licence, Share Alike
  7.  * http://creativecommons.org/licenses/sa/1.0/
  8.  */
  9.  
  10. /**
  11.  * Creates a Month object
  12.  * @param abbr - The month abbreviation, usually the first 3 or 4 letters.
  13.  * @param full - The full month name.
  14.  */
  15. function month(abbr, full) {
  16.         this.abbr = abbr;
  17.         this.full = full;
  18.         this.days = days;
  19. }
  20.  
  21. /**
  22.  * Creates a Day object
  23.  * @param abbr - The day abbreviation, usually the first 3 or 4 letters.
  24.  * @param full - The full day name.
  25.  */
  26. function day(abbr, full) {
  27.         this.abbr = abbr;
  28.         this.full = full;
  29. }
  30.  
  31. /*
  32.  * Configuration
  33.  * These variables control various aspects of the date control,
  34.  * including the Month and Day names/abbreviations; and date format
  35.  */
  36. var days = new Array(new day("Sun", "Sunday"),
  37.                      new day("Mon", "Monday"),
  38.                      new day("Tue", "Tuesday"),
  39.                      new day("Wed", "Wednesday"),
  40.                      new day("Thu", "Thursday"),
  41.                      new day("Fri", "Friday"),
  42.                      new day("Sat", "Saturday"));
  43.  
  44. var months = new Array(new month("Jan", "January"),
  45.                        new month("Feb", "February"),
  46.                        new month("Mar", "March"),
  47.                        new month("Apr", "April"),
  48.                        new month("May", "May"),
  49.                        new month("Jun", "June"),
  50.                        new month("Jul", "July"),
  51.                        new month("Aug", "August"),
  52.                        new month("Sep", "September"),
  53.                        new month("Oct", "October"),
  54.                        new month("Nov", "November"),
  55.                        new month("Dec", "December"));
  56.  
  57.  
  58. parseISO8601("2004-06-28T12:30:15.327+05:00");
  59.  
  60. /**
  61.  * Parses a datetime string in ISO 8601:2000 format and returns a Date object
  62.  * This accepts a string representing a single date and or time in
  63.  * ISO 8601:2000 format.  This does not accept datetime ranges.
  64.  * Any field that is omitted, or cannot be parsed correctly from the
  65.  * <code>datetime</code>, is set to a default value.  The default values are
  66.  * set as follows:
  67.  *
  68.  * The year, month and day default to the UTC date according to the current
  69.  * system date that this is being executed on.  The hour, minute, seconds and
  70.  * second fraction are set to zero (0), and the timezone offset defaults to
  71.  * UTC/GMT.
  72.  *
  73.  * This function accepts dates in the following formats:
  74.  * YYYY-MM-DD
  75.  * YYYY-MM-DDThh:mmZ
  76.  * YYYY-MM-DDThh:mm:ssZ
  77.  * YYYY-MM-DDThh:mm:ss.tZ
  78.  * Where MM-DD can be replaced with Www-D, T can be replaced with white space,
  79.  * and Z can be replaced by a + or - followed by hh:mm, and where the letters
  80.  * are to be interpreted as follows:
  81.  *
  82.  * YYYY:  4 or more digit year
  83.  * MM:    2 digit month in the range 1 to 12
  84.  * W:     Literal 'W'
  85.  * ww:    2 digit week in the range 1 to 53
  86.  * DD:    2 digit day of month, in the range 1 to 31, with the condition
  87.  *        that it is less than or equal to the number ofdays in the month
  88.  * D:     1 digit day representing the day of the week, in the range 1 to 7
  89.  * hh:    2 digit hour, in 24 hour time, in the range 00 to 23
  90.  * mm:    2 digit minute, in the range 00 to 59
  91.  * ss:    2 digit second, in the range 00 to 59
  92.  * t:     1 or more digits representing the second fraction
  93.  * Z:     Literal 'Z' interpreted as UTC timezone.
  94.  */
  95. function parseISO8601(datetime) {
  96.         var year, month, isWeek, week, dayOfMonth, dayOfWeek, hour, minute, second, secfrac, timezoneOffset;
  97.         var dtStr; // Holds the result string after concatenation of the fields into IETF RFC822 format
  98.  
  99.         var re = /^(?:(d{4,})-([Ww])?(d{2})-(d{1,2}))?[Tts]?(?:(d{2}):(d{2})(?::(d{2})(?:.(d+))?)?(Z|[+-]d{2}:d{2}))?/
  100.         var dt = re.exec(datetime)
  101.         var def = new Date();  // Date to generate default values for year, month and date.
  102.  
  103.         var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
  104.  
  105.         year = dt[1] != undefined? dt[1] : def.getUTCFullYear();
  106.         isWeek = (dt[2].toUpperCase() == "W");
  107.  
  108.         // If first char is W or w, then the field represents a week number
  109.         if (isWeek) {
  110.                 if (dt[3] < 1) {
  111.                         week = 1;
  112.                 }
  113.                 else if (dt[3] > 53) {
  114.                         week = 53;
  115.                 }
  116.         }
  117.         else {
  118.         month = dt[2] != undefined? dt[2] : def.getUTCMonth();
  119.         }
  120.         date = dt[4] != undefined? dt[4] : def.getUTCDate();
  121.         hour = dt[5] != undefined? dt[5] : "00";
  122.         minute = dt[6] != undefined? dt[6] : "00";
  123.         seconds = dt[7] != undefined? dt[7] : "00";
  124.         secfrac = dt[8] != undefined? dt[8] : "0";
  125.         timezoneOffset = (dt[9] != "Z" && dt[9] != undefined)? "GMT" + (new String(dt[8]).replace(/([+-])(d{2}):(d{2})/, "$1$2$3")) : "GMT";
  126.  
  127.         dtStr = date + " " + months[Number(month)] + " " + year + " "
  128.                   + hour + ":" + minute + ":" + seconds + " "
  129.                           + timezoneOffset;
  130.         document.write(new Date(Date.parse(dtStr)).toUTCString());
  131. }
Parsed in 0.024 seconds