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 2008-07-26 / dscott@laurentian.ca
25 - Comment out Date.parseIso8601 as we move to Dojo
27 2007-02-02 / billserickson@gmail.com
28 - chopped out some utility methods to trim file size
29 - changed some formatting for visual ease
30 - date / time can now be separated by a "T", "t" or a space
31 - truncating milliseconds. not needed and .123456
32 comes accross 123456ms and not 123ms + 456 microseconds
36 Date.parseIso8601 = function(CurDate) {
38 // Check the input parameters
39 if ( typeof CurDate != "string" ) {
42 // Set the fragment expressions
44 var Yr = "((?:1[6-9]|[2-9][0-9])[0-9]{2})";
45 var Mo = S + "((?:1[012])|(?:0[1-9])|[1-9])";
46 var Dy = S + "((?:3[01])|(?:[12][0-9])|(?:0[1-9])|[1-9])";
47 var Hr = "(2[0-4]|[01]?[0-9])";
48 var Mn = S + "([0-5]?[0-9])";
49 var Sd = "(?:" + S + "([0-5]?[0-9])(?:[.,]([0-9]+))?)?";
50 var TZ = "(?:(Z)|(?:([\+\-])(1[012]|[0]?[0-9])(?::?([0-5]?[0-9]))?))?";
52 // First check: Just date parts (month and day are optional)
53 // Second check: Full date plus time (seconds, milliseconds and TimeZone info are optional)
56 if ( TF = new RegExp("^" + Yr + "(?:" + Mo + "(?:" + Dy + ")?)?" + "$").exec(CurDate) ) {
57 } else if ( TF = new RegExp("^" + Yr + Mo + Dy + "[Tt ]" + Hr + Mn + Sd + TZ + "$").exec(CurDate) ) {};
59 // If the date couldn't be parsed, return null
60 if ( !TF ) { return null };
61 // Default the Time Fragments if they're not present
62 if ( !TF[2] ) { TF[2] = 1 } else { TF[2] = TF[2] - 1 };
63 if ( !TF[3] ) { TF[3] = 1 };
64 if ( !TF[4] ) { TF[4] = 0 };
65 if ( !TF[5] ) { TF[5] = 0 };
66 if ( !TF[6] ) { TF[6] = 0 };
67 if ( !TF[7] ) { TF[7] = 0 };
68 if ( !TF[8] ) { TF[8] = null };
69 if ( TF[9] != "-" && TF[9] != "+" ) { TF[9] = null };
70 if ( !TF[10] ) { TF[10] = 0 } else { TF[10] = TF[9] + TF[10] };
71 if ( !TF[11] ) { TF[11] = 0 } else { TF[11] = TF[9] + TF[11] };
72 // If there's no timezone info the data is local time
76 if ( !TF[8] && !TF[9] ) {
77 return new Date(TF[1], TF[2], TF[3], TF[4], TF[5], TF[6], TF[7]);
79 // If the UTC indicator is set the date is UTC
81 return new Date(Date.UTC(TF[1], TF[2], TF[3], TF[4], TF[5], TF[6], TF[7]));
83 // If the date has a timezone offset
84 if ( TF[9] == "-" || TF[9] == "+" ) {
85 // Get current Timezone information
86 var CurTZ = new Date().getTimezoneOffset();
87 var CurTZh = TF[10] - ((CurTZ >= 0 ? "-" : "+") + Math.floor(Math.abs(CurTZ) / 60))
88 var CurTZm = TF[11] - ((CurTZ >= 0 ? "-" : "+") + (Math.abs(CurTZ) % 60))
90 return new Date(TF[1], TF[2], TF[3], TF[4] - CurTZh, TF[5] - CurTZm, TF[6], TF[7]);
92 // If we've reached here we couldn't deal with the input, return null
100 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
101 /* "Date" Object Prototype Extensions */
102 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
104 Date.prototype.dateFormat = function(Mask) {
106 var FormattedDate = "";
107 var Ref_MonthFullName = ["January", "February", "March", "April", "May",
108 "June", "July", "August", "September", "October", "November", "December"];
109 var Ref_MonthAbbreviation = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
110 var Ref_DayFullName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
111 var Ref_DayAbbreviation = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
113 // Convert any supported simple masks into "real" masks
119 Mask = "mmm d, yyyy";
122 Mask = "mmmm d, yyyy";
125 Mask = "dddd, mmmm d, yyyy";
129 // Tack a temporary space at the end of the mask to ensure that the last character isn't a mask character
135 for ( var Cnt = 0; Cnt < Mask.length; Cnt++ ) {
137 CurChar = Mask.charAt(Cnt);
138 // Determine if the character is a mask element
139 if ( (CurChar != "d") && (CurChar != "m") && (CurChar != "y") ) {
140 // Determine if we need to parse a MaskPart or not
141 if ( MaskPart != "" ) {
142 // Convert the mask part to the date value
145 FormattedDate += this.getDate();
148 FormattedDate += ("0" + this.getDate()).slice(-2);
151 FormattedDate += Ref_DayAbbreviation[this.getDay()];
154 FormattedDate += Ref_DayFullName[this.getDay()];
157 FormattedDate += this.getMonth() + 1;
160 FormattedDate += ("0" + (this.getMonth() + 1)).slice(-2);
163 FormattedDate += Ref_MonthAbbreviation[this.getMonth()];
166 FormattedDate += Ref_MonthFullName[this.getMonth()];
169 FormattedDate += ("0" + this.getFullYear()).slice(-2);
172 FormattedDate += ("000" + this.getFullYear()).slice(-4);
175 // Reset the MaskPart to nothing
178 // Add the character to the output
179 FormattedDate += CurChar;
181 // Add the current mask character to the MaskPart
186 // Remove the temporary space from the end of the formatted date
187 FormattedDate = FormattedDate.substring(0,FormattedDate.length - 1);
189 // Return the formatted date
190 return FormattedDate;
195 Date.prototype.timeFormat = function(Mask) {
197 var FormattedTime = "";
199 // Convert any supported simple masks into "real" masks
208 Mask = "h:mm:ss.l tt";
211 Mask = "h:mm:ss.l tt";
215 // Tack a temporary space at the end of the mask to ensure that the last character isn't a mask character
221 for ( var Cnt = 0; Cnt < Mask.length; Cnt++ ) {
223 CurChar = Mask.charAt(Cnt);
224 // Determine if the character is a mask element
225 if ( (CurChar != "h") && (CurChar != "H") && (CurChar != "m") &&
226 (CurChar != "s") && (CurChar != "l") && (CurChar != "t") && (CurChar != "T") ) {
227 // Determine if we need to parse a MaskPart or not
228 if ( MaskPart != "" ) {
229 // Convert the mask part to the date value
232 var CurValue = this.getHours();
233 if ( CurValue > 12 ) {
234 CurValue = CurValue - 12;
236 FormattedTime += CurValue;
239 var CurValue = this.getHours();
240 if ( CurValue > 12 ) {
241 CurValue = CurValue - 12;
243 FormattedTime += ("0" + CurValue).slice(-2);
246 FormattedTime += ("0" + this.getHours()).slice(-2);
249 FormattedTime += ("0" + this.getHours()).slice(-2);
252 FormattedTime += this.getMinutes();
255 FormattedTime += ("0" + this.getMinutes()).slice(-2);
258 FormattedTime += this.getSeconds();
261 FormattedTime += ("0" + this.getSeconds()).slice(-2);
264 FormattedTime += ("00" + this.getMilliseconds()).slice(-3);
267 if ( this.getHours() > 12 ) {
268 FormattedTime += "p";
270 FormattedTime += "a";
274 if ( this.getHours() > 12 ) {
275 FormattedTime += "pm";
277 FormattedTime += "am";
281 if ( this.getHours() > 12 ) {
282 FormattedTime += "P";
284 FormattedTime += "A";
288 if ( this.getHours() > 12 ) {
289 FormattedTime += "PM";
291 FormattedTime += "AM";
295 // Reset the MaskPart to nothing
298 // Add the character to the output
299 FormattedTime += CurChar;
301 // Add the current mask character to the MaskPart
306 // Remove the temporary space from the end of the formatted date
307 FormattedTime = FormattedTime.substring(0,FormattedTime.length - 1);
309 // Return the formatted date
310 return FormattedTime;
316 dropTZ - do not include the timezone in the output. only used for YMDH+
317 useSpace - if true, use a space instaed of a "T" between date and time
319 Date.prototype.iso8601Format = function(Style, isUTC, dropTZ, useSpace) {
321 var FormattedDate = "";
325 FormattedDate += this.dateFormat("yyyy");
328 FormattedDate += this.dateFormat("yyyy-mm");
331 FormattedDate += this.dateFormat("yyyy-mm-dd");
334 FormattedDate += this.dateFormat("yyyy-mm-dd") + ((useSpace) ? " " : "T") + this.timeFormat("HH:mm");
337 FormattedDate += this.dateFormat("yyyy-mm-dd") + ((useSpace) ? " " : "T") + this.timeFormat("HH:mm:ss");
340 FormattedDate += this.dateFormat("yyyy-mm-dd") + ((useSpace) ? " " : "T") + this.timeFormat("HH:mm:ss.l");
344 if ( !dropTZ && (Style == "YMDHM" || Style == "YMDHMS" || Style == "YMDHMSM") ) {
346 FormattedDate += "Z";
348 // Get TimeZone Information
349 var TimeZoneOffset = this.getTimezoneOffset();
350 var TimeZoneInfo = (TimeZoneOffset >= 0 ? "-" : "+") +
351 ("0" + (Math.floor(Math.abs(TimeZoneOffset) / 60))).slice(-2) + ":" +
352 ("00" + (Math.abs(TimeZoneOffset) % 60)).slice(-2);
353 FormattedDate += TimeZoneInfo;
358 return FormattedDate;