/** * ISO 8601:2000 Date Object Extensions * @author Lachlan Hunt * @version 1.0 2004-06-25 * * This script is licenced under a creative commons licence, Share Alike * http://creativecommons.org/licenses/sa/1.0/ */ /** * Creates a Month object * @param abbr - The month abbreviation, usually the first 3 or 4 letters. * @param full - The full month name. */ function month(abbr, full) { this.abbr = abbr; this.full = full; this.days = days; } /** * Creates a Day object * @param abbr - The day abbreviation, usually the first 3 or 4 letters. * @param full - The full day name. */ function day(abbr, full) { this.abbr = abbr; this.full = full; } /* * Configuration * These variables control various aspects of the date control, * including the Month and Day names/abbreviations; and date format */ var days = new Array(new day("Sun", "Sunday"), new day("Mon", "Monday"), new day("Tue", "Tuesday"), new day("Wed", "Wednesday"), new day("Thu", "Thursday"), new day("Fri", "Friday"), new day("Sat", "Saturday")); var months = new Array(new month("Jan", "January"), new month("Feb", "February"), new month("Mar", "March"), new month("Apr", "April"), new month("May", "May"), new month("Jun", "June"), new month("Jul", "July"), new month("Aug", "August"), new month("Sep", "September"), new month("Oct", "October"), new month("Nov", "November"), new month("Dec", "December")); parseISO8601("2004-06-28T12:30:15.327+05:00"); /** * Parses a datetime string in ISO 8601:2000 format and returns a Date object * This accepts a string representing a single date and or time in * ISO 8601:2000 format. This does not accept datetime ranges. * Any field that is omitted, or cannot be parsed correctly from the * datetime, is set to a default value. The default values are * set as follows: * * The year, month and day default to the UTC date according to the current * system date that this is being executed on. The hour, minute, seconds and * second fraction are set to zero (0), and the timezone offset defaults to * UTC/GMT. * * This function accepts dates in the following formats: * YYYY-MM-DD * YYYY-MM-DDThh:mmZ * YYYY-MM-DDThh:mm:ssZ * YYYY-MM-DDThh:mm:ss.tZ * Where MM-DD can be replaced with Www-D, T can be replaced with white space, * and Z can be replaced by a + or - followed by hh:mm, and where the letters * are to be interpreted as follows: * * YYYY: 4 or more digit year * MM: 2 digit month in the range 1 to 12 * W: Literal 'W' * ww: 2 digit week in the range 1 to 53 * DD: 2 digit day of month, in the range 1 to 31, with the condition * that it is less than or equal to the number ofdays in the month * D: 1 digit day representing the day of the week, in the range 1 to 7 * hh: 2 digit hour, in 24 hour time, in the range 00 to 23 * mm: 2 digit minute, in the range 00 to 59 * ss: 2 digit second, in the range 00 to 59 * t: 1 or more digits representing the second fraction * Z: Literal 'Z' interpreted as UTC timezone. */ function parseISO8601(datetime) { var year, month, isWeek, week, dayOfMonth, dayOfWeek, hour, minute, second, secfrac, timezoneOffset; var dtStr; // Holds the result string after concatenation of the fields into IETF RFC822 format var re = /^(?:(\d{4,})-([Ww])?(\d{2})-(\d{1,2}))?[Tt\s]?(?:(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|[+-]\d{2}:\d{2}))?/ var dt = re.exec(datetime) var def = new Date(); // Date to generate default values for year, month and date. var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); year = dt[1] != undefined? dt[1] : def.getUTCFullYear(); isWeek = (dt[2].toUpperCase() == "W"); // If first char is W or w, then the field represents a week number if (isWeek) { if (dt[3] < 1) { week = 1; } else if (dt[3] > 53) { week = 53; } } else { month = dt[2] != undefined? dt[2] : def.getUTCMonth(); } date = dt[4] != undefined? dt[4] : def.getUTCDate(); hour = dt[5] != undefined? dt[5] : "00"; minute = dt[6] != undefined? dt[6] : "00"; seconds = dt[7] != undefined? dt[7] : "00"; secfrac = dt[8] != undefined? dt[8] : "0"; timezoneOffset = (dt[9] != "Z" && dt[9] != undefined)? "GMT" + (new String(dt[8]).replace(/([+-])(\d{2}):(\d{2})/, "$1$2$3")) : "GMT"; dtStr = date + " " + months[Number(month)] + " " + year + " " + hour + ":" + minute + ":" + seconds + " " + timezoneOffset; document.write(new Date(Date.parse(dtStr)).toUTCString()); }