1 /* DepressedPress.com DP_DateExtensions
2 Author: Jim Davis, the Depressed Press of Boston
4 Contact: webmaster@depressedpress.com
5 Website: www.depressedpress.com
7 Full documentation can be found at:
8 http://www.depressedpress.com/Content/Development/JavaScript/Extensions/
10 DP_DateExtensions adds features to the JavaScript "Date" datatype.
11 Copyright (c) 1996-2006, The Depressed Press of Boston (depressedpress.com)
13 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
15 +) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
16 +) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
17 +) Neither the name of the DEPRESSED PRESS OF BOSTON (DEPRESSEDPRESS.COM) nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 CHANGES: --------------------------------------------------------------------------------
24 2007-02-02 / billserickson@gmail.com
25 - chopped out some utility methods to trim file size
26 - changed some formatting for visual ease
27 - date / time can now be separated by a "T", "t" or a space
28 - truncating milliseconds. not needed and .123456
29 comes accross 123456ms and not 123ms + 456 microseconds
33 Date.parseIso8601 = function(CurDate) {
35 // Check the input parameters
36 if ( typeof CurDate != "string" ) {
39 // Set the fragment expressions
41 var Yr = "((?:1[6-9]|[2-9][0-9])[0-9]{2})";
42 var Mo = S + "((?:1[012])|(?:0[1-9])|[1-9])";
43 var Dy = S + "((?:3[01])|(?:[12][0-9])|(?:0[1-9])|[1-9])";
44 var Hr = "(2[0-4]|[01]?[0-9])";
45 var Mn = S + "([0-5]?[0-9])";
46 var Sd = "(?:" + S + "([0-5]?[0-9])(?:[.,]([0-9]+))?)?";
47 var TZ = "(?:(Z)|(?:([\+\-])(1[012]|[0]?[0-9])(?::?([0-5]?[0-9]))?))?";
49 // First check: Just date parts (month and day are optional)
50 // Second check: Full date plus time (seconds, milliseconds and TimeZone info are optional)
53 if ( TF = new RegExp("^" + Yr + "(?:" + Mo + "(?:" + Dy + ")?)?" + "$").exec(CurDate) ) {
54 } else if ( TF = new RegExp("^" + Yr + Mo + Dy + "[Tt ]" + Hr + Mn + Sd + TZ + "$").exec(CurDate) ) {};
56 // If the date couldn't be parsed, return null
57 if ( !TF ) { return null };
58 // Default the Time Fragments if they're not present
59 if ( !TF[2] ) { TF[2] = 1 } else { TF[2] = TF[2] - 1 };
60 if ( !TF[3] ) { TF[3] = 1 };
61 if ( !TF[4] ) { TF[4] = 0 };
62 if ( !TF[5] ) { TF[5] = 0 };
63 if ( !TF[6] ) { TF[6] = 0 };
64 if ( !TF[7] ) { TF[7] = 0 };
65 if ( !TF[8] ) { TF[8] = null };
66 if ( TF[9] != "-" && TF[9] != "+" ) { TF[9] = null };
67 if ( !TF[10] ) { TF[10] = 0 } else { TF[10] = TF[9] + TF[10] };
68 if ( !TF[11] ) { TF[11] = 0 } else { TF[11] = TF[9] + TF[11] };
69 // If there's no timezone info the data is local time
73 if ( !TF[8] && !TF[9] ) {
74 return new Date(TF[1], TF[2], TF[3], TF[4], TF[5], TF[6], TF[7]);
76 // If the UTC indicator is set the date is UTC
78 return new Date(Date.UTC(TF[1], TF[2], TF[3], TF[4], TF[5], TF[6], TF[7]));
80 // If the date has a timezone offset
81 if ( TF[9] == "-" || TF[9] == "+" ) {
82 // Get current Timezone information
83 var CurTZ = new Date().getTimezoneOffset();
84 var CurTZh = TF[10] - ((CurTZ >= 0 ? "-" : "+") + Math.floor(Math.abs(CurTZ) / 60))
85 var CurTZm = TF[11] - ((CurTZ >= 0 ? "-" : "+") + (Math.abs(CurTZ) % 60))
87 return new Date(TF[1], TF[2], TF[3], TF[4] - CurTZh, TF[5] - CurTZm, TF[6], TF[7]);
89 // If we've reached here we couldn't deal with the input, return null
95 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
96 /* "Date" Object Prototype Extensions */
97 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99 Date.prototype.dateFormat = function(Mask) {
101 var FormattedDate = "";
102 var Ref_MonthFullName = ["January", "February", "March", "April", "May",
103 "June", "July", "August", "September", "October", "November", "December"];
104 var Ref_MonthAbbreviation = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
105 var Ref_DayFullName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
106 var Ref_DayAbbreviation = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
108 // Convert any supported simple masks into "real" masks
114 Mask = "mmm d, yyyy";
117 Mask = "mmmm d, yyyy";
120 Mask = "dddd, mmmm d, yyyy";
124 // Tack a temporary space at the end of the mask to ensure that the last character isn't a mask character
130 for ( var Cnt = 0; Cnt < Mask.length; Cnt++ ) {
132 CurChar = Mask.charAt(Cnt);
133 // Determine if the character is a mask element
134 if ( (CurChar != "d") && (CurChar != "m") && (CurChar != "y") ) {
135 // Determine if we need to parse a MaskPart or not
136 if ( MaskPart != "" ) {
137 // Convert the mask part to the date value
140 FormattedDate += this.getDate();
143 FormattedDate += ("0" + this.getDate()).slice(-2);
146 FormattedDate += Ref_DayAbbreviation[this.getDay()];
149 FormattedDate += Ref_DayFullName[this.getDay()];
152 FormattedDate += this.getMonth() + 1;
155 FormattedDate += ("0" + (this.getMonth() + 1)).slice(-2);
158 FormattedDate += Ref_MonthAbbreviation[this.getMonth()];
161 FormattedDate += Ref_MonthFullName[this.getMonth()];
164 FormattedDate += ("0" + this.getFullYear()).slice(-2);
167 FormattedDate += ("000" + this.getFullYear()).slice(-4);
170 // Reset the MaskPart to nothing
173 // Add the character to the output
174 FormattedDate += CurChar;
176 // Add the current mask character to the MaskPart
181 // Remove the temporary space from the end of the formatted date
182 FormattedDate = FormattedDate.substring(0,FormattedDate.length - 1);
184 // Return the formatted date
185 return FormattedDate;
190 Date.prototype.timeFormat = function(Mask) {
192 var FormattedTime = "";
194 // Convert any supported simple masks into "real" masks
203 Mask = "h:mm:ss.l tt";
206 Mask = "h:mm:ss.l tt";
210 // Tack a temporary space at the end of the mask to ensure that the last character isn't a mask character
216 for ( var Cnt = 0; Cnt < Mask.length; Cnt++ ) {
218 CurChar = Mask.charAt(Cnt);
219 // Determine if the character is a mask element
220 if ( (CurChar != "h") && (CurChar != "H") && (CurChar != "m") &&
221 (CurChar != "s") && (CurChar != "l") && (CurChar != "t") && (CurChar != "T") ) {
222 // Determine if we need to parse a MaskPart or not
223 if ( MaskPart != "" ) {
224 // Convert the mask part to the date value
227 var CurValue = this.getHours();
228 if ( CurValue > 12 ) {
229 CurValue = CurValue - 12;
231 FormattedTime += CurValue;
234 var CurValue = this.getHours();
235 if ( CurValue > 12 ) {
236 CurValue = CurValue - 12;
238 FormattedTime += ("0" + CurValue).slice(-2);
241 FormattedTime += ("0" + this.getHours()).slice(-2);
244 FormattedTime += ("0" + this.getHours()).slice(-2);
247 FormattedTime += this.getMinutes();
250 FormattedTime += ("0" + this.getMinutes()).slice(-2);
253 FormattedTime += this.getSeconds();
256 FormattedTime += ("0" + this.getSeconds()).slice(-2);
259 FormattedTime += ("00" + this.getMilliseconds()).slice(-3);
262 if ( this.getHours() > 12 ) {
263 FormattedTime += "p";
265 FormattedTime += "a";
269 if ( this.getHours() > 12 ) {
270 FormattedTime += "pm";
272 FormattedTime += "am";
276 if ( this.getHours() > 12 ) {
277 FormattedTime += "P";
279 FormattedTime += "A";
283 if ( this.getHours() > 12 ) {
284 FormattedTime += "PM";
286 FormattedTime += "AM";
290 // Reset the MaskPart to nothing
293 // Add the character to the output
294 FormattedTime += CurChar;
296 // Add the current mask character to the MaskPart
301 // Remove the temporary space from the end of the formatted date
302 FormattedTime = FormattedTime.substring(0,FormattedTime.length - 1);
304 // Return the formatted date
305 return FormattedTime;
311 dropTZ - do not include the timezone in the output. only used for YMDH+
312 useSpace - if true, use a space instaed of a "T" between date and time
314 Date.prototype.iso8601Format = function(Style, isUTC, dropTZ, useSpace) {
316 var FormattedDate = "";
320 FormattedDate += this.dateFormat("yyyy");
323 FormattedDate += this.dateFormat("yyyy-mm");
326 FormattedDate += this.dateFormat("yyyy-mm-dd");
329 FormattedDate += this.dateFormat("yyyy-mm-dd") + ((useSpace) ? " " : "T") + this.timeFormat("HH:mm");
332 FormattedDate += this.dateFormat("yyyy-mm-dd") + ((useSpace) ? " " : "T") + this.timeFormat("HH:mm:ss");
335 FormattedDate += this.dateFormat("yyyy-mm-dd") + ((useSpace) ? " " : "T") + this.timeFormat("HH:mm:ss.l");
339 if ( !dropTZ && (Style == "YMDHM" || Style == "YMDHMS" || Style == "YMDHMSM") ) {
341 FormattedDate += "Z";
343 // Get TimeZone Information
344 var TimeZoneOffset = this.getTimezoneOffset();
345 var TimeZoneInfo = (TimeZoneOffset >= 0 ? "-" : "+") +
346 ("0" + (Math.floor(Math.abs(TimeZoneOffset) / 60))).slice(-2) + ":" +
347 ("00" + (Math.abs(TimeZoneOffset) % 60)).slice(-2);
348 FormattedDate += TimeZoneInfo;
353 return FormattedDate;