michael@0: /* michael@0: * Copyright (C) 2004 Baron Schwartz michael@0: * michael@0: * This program is free software; you can redistribute it and/or modify it michael@0: * under the terms of the GNU Lesser General Public License as published by the michael@0: * Free Software Foundation, version 2.1. michael@0: * michael@0: * This program is distributed in the hope that it will be useful, but WITHOUT michael@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS michael@0: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more michael@0: * details. michael@0: */ michael@0: michael@0: Date.parseFunctions = {count:0}; michael@0: Date.parseRegexes = []; michael@0: Date.formatFunctions = {count:0}; michael@0: michael@0: Date.prototype.dateFormat = function(format) { michael@0: if (Date.formatFunctions[format] == null) { michael@0: Date.createNewFormat(format); michael@0: } michael@0: var func = Date.formatFunctions[format]; michael@0: return this[func](); michael@0: } michael@0: michael@0: Date.createNewFormat = function(format) { michael@0: var funcName = "format" + Date.formatFunctions.count++; michael@0: Date.formatFunctions[format] = funcName; michael@0: var code = "Date.prototype." + funcName + " = function(){return "; michael@0: var special = false; michael@0: var ch = ''; michael@0: for (var i = 0; i < format.length; ++i) { michael@0: ch = format.charAt(i); michael@0: if (!special && ch == "\\") { michael@0: special = true; michael@0: } michael@0: else if (special) { michael@0: special = false; michael@0: code += "'" + String.escape(ch) + "' + "; michael@0: } michael@0: else { michael@0: code += Date.getFormatCode(ch); michael@0: } michael@0: } michael@0: eval(code.substring(0, code.length - 3) + ";}"); michael@0: } michael@0: michael@0: Date.getFormatCode = function(character) { michael@0: switch (character) { michael@0: case "d": michael@0: return "String.leftPad(this.getDate(), 2, '0') + "; michael@0: case "D": michael@0: return "Date.dayNames[this.getDay()].substring(0, 3) + "; michael@0: case "j": michael@0: return "this.getDate() + "; michael@0: case "l": michael@0: return "Date.dayNames[this.getDay()] + "; michael@0: case "S": michael@0: return "this.getSuffix() + "; michael@0: case "w": michael@0: return "this.getDay() + "; michael@0: case "z": michael@0: return "this.getDayOfYear() + "; michael@0: case "W": michael@0: return "this.getWeekOfYear() + "; michael@0: case "F": michael@0: return "Date.monthNames[this.getMonth()] + "; michael@0: case "m": michael@0: return "String.leftPad(this.getMonth() + 1, 2, '0') + "; michael@0: case "M": michael@0: return "Date.monthNames[this.getMonth()].substring(0, 3) + "; michael@0: case "n": michael@0: return "(this.getMonth() + 1) + "; michael@0: case "t": michael@0: return "this.getDaysInMonth() + "; michael@0: case "L": michael@0: return "(this.isLeapYear() ? 1 : 0) + "; michael@0: case "Y": michael@0: return "this.getFullYear() + "; michael@0: case "y": michael@0: return "('' + this.getFullYear()).substring(2, 4) + "; michael@0: case "a": michael@0: return "(this.getHours() < 12 ? 'am' : 'pm') + "; michael@0: case "A": michael@0: return "(this.getHours() < 12 ? 'AM' : 'PM') + "; michael@0: case "g": michael@0: return "((this.getHours() %12) ? this.getHours() % 12 : 12) + "; michael@0: case "G": michael@0: return "this.getHours() + "; michael@0: case "h": michael@0: return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + "; michael@0: case "H": michael@0: return "String.leftPad(this.getHours(), 2, '0') + "; michael@0: case "i": michael@0: return "String.leftPad(this.getMinutes(), 2, '0') + "; michael@0: case "s": michael@0: return "String.leftPad(this.getSeconds(), 2, '0') + "; michael@0: case "O": michael@0: return "this.getGMTOffset() + "; michael@0: case "T": michael@0: return "this.getTimezone() + "; michael@0: case "Z": michael@0: return "(this.getTimezoneOffset() * -60) + "; michael@0: default: michael@0: return "'" + String.escape(character) + "' + "; michael@0: } michael@0: } michael@0: michael@0: Date.parseDate = function(input, format) { michael@0: if (Date.parseFunctions[format] == null) { michael@0: Date.createParser(format); michael@0: } michael@0: var func = Date.parseFunctions[format]; michael@0: return Date[func](input); michael@0: } michael@0: michael@0: Date.createParser = function(format) { michael@0: var funcName = "parse" + Date.parseFunctions.count++; michael@0: var regexNum = Date.parseRegexes.length; michael@0: var currentGroup = 1; michael@0: Date.parseFunctions[format] = funcName; michael@0: michael@0: var code = "Date." + funcName + " = function(input){\n" michael@0: + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1;\n" michael@0: + "var d = new Date();\n" michael@0: + "y = d.getFullYear();\n" michael@0: + "m = d.getMonth();\n" michael@0: + "d = d.getDate();\n" michael@0: + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n" michael@0: + "if (results && results.length > 0) {" michael@0: var regex = ""; michael@0: michael@0: var special = false; michael@0: var ch = ''; michael@0: for (var i = 0; i < format.length; ++i) { michael@0: ch = format.charAt(i); michael@0: if (!special && ch == "\\") { michael@0: special = true; michael@0: } michael@0: else if (special) { michael@0: special = false; michael@0: regex += String.escape(ch); michael@0: } michael@0: else { michael@0: obj = Date.formatCodeToRegex(ch, currentGroup); michael@0: currentGroup += obj.g; michael@0: regex += obj.s; michael@0: if (obj.g && obj.c) { michael@0: code += obj.c; michael@0: } michael@0: } michael@0: } michael@0: michael@0: code += "if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n" michael@0: + "{return new Date(y, m, d, h, i, s);}\n" michael@0: + "else if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n" michael@0: + "{return new Date(y, m, d, h, i);}\n" michael@0: + "else if (y > 0 && m >= 0 && d > 0 && h >= 0)\n" michael@0: + "{return new Date(y, m, d, h);}\n" michael@0: + "else if (y > 0 && m >= 0 && d > 0)\n" michael@0: + "{return new Date(y, m, d);}\n" michael@0: + "else if (y > 0 && m >= 0)\n" michael@0: + "{return new Date(y, m);}\n" michael@0: + "else if (y > 0)\n" michael@0: + "{return new Date(y);}\n" michael@0: + "}return null;}"; michael@0: michael@0: Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$"); michael@0: eval(code); michael@0: } michael@0: michael@0: Date.formatCodeToRegex = function(character, currentGroup) { michael@0: switch (character) { michael@0: case "D": michael@0: return {g:0, michael@0: c:null, michael@0: s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"}; michael@0: case "j": michael@0: case "d": michael@0: return {g:1, michael@0: c:"d = parseInt(results[" + currentGroup + "], 10);\n", michael@0: s:"(\\d{1,2})"}; michael@0: case "l": michael@0: return {g:0, michael@0: c:null, michael@0: s:"(?:" + Date.dayNames.join("|") + ")"}; michael@0: case "S": michael@0: return {g:0, michael@0: c:null, michael@0: s:"(?:st|nd|rd|th)"}; michael@0: case "w": michael@0: return {g:0, michael@0: c:null, michael@0: s:"\\d"}; michael@0: case "z": michael@0: return {g:0, michael@0: c:null, michael@0: s:"(?:\\d{1,3})"}; michael@0: case "W": michael@0: return {g:0, michael@0: c:null, michael@0: s:"(?:\\d{2})"}; michael@0: case "F": michael@0: return {g:1, michael@0: c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "].substring(0, 3)], 10);\n", michael@0: s:"(" + Date.monthNames.join("|") + ")"}; michael@0: case "M": michael@0: return {g:1, michael@0: c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "]], 10);\n", michael@0: s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"}; michael@0: case "n": michael@0: case "m": michael@0: return {g:1, michael@0: c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n", michael@0: s:"(\\d{1,2})"}; michael@0: case "t": michael@0: return {g:0, michael@0: c:null, michael@0: s:"\\d{1,2}"}; michael@0: case "L": michael@0: return {g:0, michael@0: c:null, michael@0: s:"(?:1|0)"}; michael@0: case "Y": michael@0: return {g:1, michael@0: c:"y = parseInt(results[" + currentGroup + "], 10);\n", michael@0: s:"(\\d{4})"}; michael@0: case "y": michael@0: return {g:1, michael@0: c:"var ty = parseInt(results[" + currentGroup + "], 10);\n" michael@0: + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n", michael@0: s:"(\\d{1,2})"}; michael@0: case "a": michael@0: return {g:1, michael@0: c:"if (results[" + currentGroup + "] == 'am') {\n" michael@0: + "if (h == 12) { h = 0; }\n" michael@0: + "} else { if (h < 12) { h += 12; }}", michael@0: s:"(am|pm)"}; michael@0: case "A": michael@0: return {g:1, michael@0: c:"if (results[" + currentGroup + "] == 'AM') {\n" michael@0: + "if (h == 12) { h = 0; }\n" michael@0: + "} else { if (h < 12) { h += 12; }}", michael@0: s:"(AM|PM)"}; michael@0: case "g": michael@0: case "G": michael@0: case "h": michael@0: case "H": michael@0: return {g:1, michael@0: c:"h = parseInt(results[" + currentGroup + "], 10);\n", michael@0: s:"(\\d{1,2})"}; michael@0: case "i": michael@0: return {g:1, michael@0: c:"i = parseInt(results[" + currentGroup + "], 10);\n", michael@0: s:"(\\d{2})"}; michael@0: case "s": michael@0: return {g:1, michael@0: c:"s = parseInt(results[" + currentGroup + "], 10);\n", michael@0: s:"(\\d{2})"}; michael@0: case "O": michael@0: return {g:0, michael@0: c:null, michael@0: s:"[+-]\\d{4}"}; michael@0: case "T": michael@0: return {g:0, michael@0: c:null, michael@0: s:"[A-Z]{3}"}; michael@0: case "Z": michael@0: return {g:0, michael@0: c:null, michael@0: s:"[+-]\\d{1,5}"}; michael@0: default: michael@0: return {g:0, michael@0: c:null, michael@0: s:String.escape(character)}; michael@0: } michael@0: } michael@0: michael@0: Date.prototype.getTimezone = function() { michael@0: return this.toString().replace( michael@0: /^.*? ([A-Z]{3}) [0-9]{4}.*$/, "$1").replace( michael@0: /^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/, "$1$2$3"); michael@0: } michael@0: michael@0: Date.prototype.getGMTOffset = function() { michael@0: return (this.getTimezoneOffset() > 0 ? "-" : "+") michael@0: + String.leftPad(Math.floor(this.getTimezoneOffset() / 60), 2, "0") michael@0: + String.leftPad(this.getTimezoneOffset() % 60, 2, "0"); michael@0: } michael@0: michael@0: Date.prototype.getDayOfYear = function() { michael@0: var num = 0; michael@0: Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28; michael@0: for (var i = 0; i < this.getMonth(); ++i) { michael@0: num += Date.daysInMonth[i]; michael@0: } michael@0: return num + this.getDate() - 1; michael@0: } michael@0: michael@0: Date.prototype.getWeekOfYear = function() { michael@0: // Skip to Thursday of this week michael@0: var now = this.getDayOfYear() + (4 - this.getDay()); michael@0: // Find the first Thursday of the year michael@0: var jan1 = new Date(this.getFullYear(), 0, 1); michael@0: var then = (7 - jan1.getDay() + 4); michael@0: document.write(then); michael@0: return String.leftPad(((now - then) / 7) + 1, 2, "0"); michael@0: } michael@0: michael@0: Date.prototype.isLeapYear = function() { michael@0: var year = this.getFullYear(); michael@0: return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year))); michael@0: } michael@0: michael@0: Date.prototype.getFirstDayOfMonth = function() { michael@0: var day = (this.getDay() - (this.getDate() - 1)) % 7; michael@0: return (day < 0) ? (day + 7) : day; michael@0: } michael@0: michael@0: Date.prototype.getLastDayOfMonth = function() { michael@0: var day = (this.getDay() + (Date.daysInMonth[this.getMonth()] - this.getDate())) % 7; michael@0: return (day < 0) ? (day + 7) : day; michael@0: } michael@0: michael@0: Date.prototype.getDaysInMonth = function() { michael@0: Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28; michael@0: return Date.daysInMonth[this.getMonth()]; michael@0: } michael@0: michael@0: Date.prototype.getSuffix = function() { michael@0: switch (this.getDate()) { michael@0: case 1: michael@0: case 21: michael@0: case 31: michael@0: return "st"; michael@0: case 2: michael@0: case 22: michael@0: return "nd"; michael@0: case 3: michael@0: case 23: michael@0: return "rd"; michael@0: default: michael@0: return "th"; michael@0: } michael@0: } michael@0: michael@0: String.escape = function(string) { michael@0: return string.replace(/('|\\)/g, "\\$1"); michael@0: } michael@0: michael@0: String.leftPad = function (val, size, ch) { michael@0: var result = new String(val); michael@0: if (ch == null) { michael@0: ch = " "; michael@0: } michael@0: while (result.length < size) { michael@0: result = ch + result; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]; michael@0: Date.monthNames = michael@0: ["January", michael@0: "February", michael@0: "March", michael@0: "April", michael@0: "May", michael@0: "June", michael@0: "July", michael@0: "August", michael@0: "September", michael@0: "October", michael@0: "November", michael@0: "December"]; michael@0: Date.dayNames = michael@0: ["Sunday", michael@0: "Monday", michael@0: "Tuesday", michael@0: "Wednesday", michael@0: "Thursday", michael@0: "Friday", michael@0: "Saturday"]; michael@0: Date.y2kYear = 50; michael@0: Date.monthNumbers = { michael@0: Jan:0, michael@0: Feb:1, michael@0: Mar:2, michael@0: Apr:3, michael@0: May:4, michael@0: Jun:5, michael@0: Jul:6, michael@0: Aug:7, michael@0: Sep:8, michael@0: Oct:9, michael@0: Nov:10, michael@0: Dec:11}; michael@0: Date.patterns = { michael@0: ISO8601LongPattern:"Y-m-d H:i:s", michael@0: ISO8601ShortPattern:"Y-m-d", michael@0: ShortDatePattern: "n/j/Y", michael@0: LongDatePattern: "l, F d, Y", michael@0: FullDateTimePattern: "l, F d, Y g:i:s A", michael@0: MonthDayPattern: "F d", michael@0: ShortTimePattern: "g:i A", michael@0: LongTimePattern: "g:i:s A", michael@0: SortableDateTimePattern: "Y-m-d\\TH:i:s", michael@0: UniversalSortableDateTimePattern: "Y-m-d H:i:sO", michael@0: YearMonthPattern: "F, Y"}; michael@0: michael@0: var date = new Date("1/1/2007 1:11:11"); michael@0: michael@0: var ret; michael@0: for (i = 0; i < 4000; ++i) { michael@0: var shortFormat = date.dateFormat("Y-m-d"); michael@0: var longFormat = date.dateFormat("l, F d, Y g:i:s A"); michael@0: ret = shortFormat + longFormat; michael@0: date.setTime(date.getTime() + 84266956); michael@0: } michael@0: michael@0: // No exact match because the output depends on the locale's time zone. See bug 524490. michael@0: assertEq(/^2017-09-05Tuesday, September 05, 2017 [0-9:]* AM$/.exec(ret) != null, true);