207 lines
4.9 KiB
Plaintext
207 lines
4.9 KiB
Plaintext
(function () {
|
|
var $D = Date;
|
|
|
|
/**
|
|
* @desc Converts the specified string value into its JavaScript Date equivalent using CultureInfo specific format information.
|
|
*
|
|
* Example
|
|
<pre><code>
|
|
///////////
|
|
// Dates //
|
|
///////////
|
|
|
|
// 15-Oct-2004
|
|
var d1 = Date.parse("10/15/2004");
|
|
|
|
// 15-Oct-2004
|
|
var d1 = Date.parse("15-Oct-2004");
|
|
|
|
// 15-Oct-2004
|
|
var d1 = Date.parse("2004.10.15");
|
|
|
|
//Fri Oct 15, 2004
|
|
var d1 = Date.parse("Fri Oct 15, 2004");
|
|
|
|
///////////
|
|
// Times //
|
|
///////////
|
|
|
|
// Today at 10 PM.
|
|
var d1 = Date.parse("10 PM");
|
|
|
|
// Today at 10:30 PM.
|
|
var d1 = Date.parse("10:30 P.M.");
|
|
|
|
// Today at 6 AM.
|
|
var d1 = Date.parse("06am");
|
|
|
|
/////////////////////
|
|
// Dates and Times //
|
|
/////////////////////
|
|
|
|
// 8-July-2004 @ 10:30 PM
|
|
var d1 = Date.parse("July 8th, 2004, 10:30 PM");
|
|
|
|
// 1-July-2004 @ 10:30 PM
|
|
var d1 = Date.parse("2004-07-01T22:30:00");
|
|
|
|
////////////////////
|
|
// Relative Dates //
|
|
////////////////////
|
|
|
|
// Returns today's date. The string "today" is culture specific.
|
|
var d1 = Date.parse("today");
|
|
|
|
// Returns yesterday's date. The string "yesterday" is culture specific.
|
|
var d1 = Date.parse("yesterday");
|
|
|
|
// Returns the date of the next thursday.
|
|
var d1 = Date.parse("Next thursday");
|
|
|
|
// Returns the date of the most previous monday.
|
|
var d1 = Date.parse("last monday");
|
|
|
|
// Returns today's day + one year.
|
|
var d1 = Date.parse("next year");
|
|
|
|
///////////////
|
|
// Date Math //
|
|
///////////////
|
|
|
|
// Today + 2 days
|
|
var d1 = Date.parse("t+2");
|
|
|
|
// Today + 2 days
|
|
var d1 = Date.parse("today + 2 days");
|
|
|
|
// Today + 3 months
|
|
var d1 = Date.parse("t+3m");
|
|
|
|
// Today - 1 year
|
|
var d1 = Date.parse("today - 1 year");
|
|
|
|
// Today - 1 year
|
|
var d1 = Date.parse("t-1y");
|
|
|
|
|
|
/////////////////////////////
|
|
// Partial Dates and Times //
|
|
/////////////////////////////
|
|
|
|
// July 15th of this year.
|
|
var d1 = Date.parse("July 15");
|
|
|
|
// 15th day of current day and year.
|
|
var d1 = Date.parse("15");
|
|
|
|
// July 1st of current year at 10pm.
|
|
var d1 = Date.parse("7/1 10pm");
|
|
</code></pre>
|
|
*
|
|
* @param {String} The string value to convert into a Date object [Required]
|
|
* @return {Date} A Date object or null if the string cannot be converted into a Date.
|
|
*/
|
|
var parseUtils = {
|
|
removeOrds: function (s) {
|
|
ords = s.match(/\b(\d+)(?:st|nd|rd|th)\b/); // find ordinal matches
|
|
s = ((ords && ords.length === 2) ? s.replace(ords[0], ords[1]) : s);
|
|
return s;
|
|
},
|
|
grammarParser: function (s) {
|
|
var r = null;
|
|
try {
|
|
r = $D.Grammar.start.call({}, s.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"));
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
|
|
return ((r[1].length === 0) ? r[0] : null);
|
|
},
|
|
nativeFallback: function(s) {
|
|
var t;
|
|
try {
|
|
// ok we haven't parsed it, last ditch attempt with the built-in parser.
|
|
t = Date._parse(s);
|
|
return (t || t === 0) ? new Date(t) : null;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
};
|
|
function parse (s) {
|
|
var d;
|
|
if (!s) {
|
|
return null;
|
|
}
|
|
if (s instanceof Date) {
|
|
return s.clone();
|
|
}
|
|
if (s.length >= 4 && s.charAt(0) !== "0" && s.charAt(0) !== "+"&& s.charAt(0) !== "-") { // ie: 2004 will pass, 0800 won't.
|
|
// Start with specific formats
|
|
d = $D.Parsing.ISO.parse(s) || $D.Parsing.Numeric.parse(s);
|
|
}
|
|
if (d instanceof Date && !isNaN(d.getTime())) {
|
|
return d;
|
|
} else {
|
|
// find ordinal dates (1st, 3rd, 8th, etc and remove them as they cause parsing issues)
|
|
s = $D.Parsing.Normalizer.parse(parseUtils.removeOrds(s));
|
|
d = parseUtils.grammarParser(s);
|
|
if (d !== null) {
|
|
return d;
|
|
} else {
|
|
return parseUtils.nativeFallback(s);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$D._parse) {
|
|
$D._parse = $D.parse;
|
|
}
|
|
$D.parse = parse;
|
|
|
|
Date.getParseFunction = function (fx) {
|
|
var fns = Date.Grammar.allformats(fx);
|
|
return function (s) {
|
|
var r = null;
|
|
for (var i = 0; i < fns.length; i++) {
|
|
try {
|
|
r = fns[i].call({}, s);
|
|
} catch (e) {
|
|
continue;
|
|
}
|
|
if (r[1].length === 0) {
|
|
return r[0];
|
|
}
|
|
}
|
|
return null;
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Converts the specified string value into its JavaScript Date equivalent using the specified format {String} or formats {Array} and the CultureInfo specific format information.
|
|
* The format of the string value must match one of the supplied formats exactly.
|
|
*
|
|
* Example
|
|
<pre><code>
|
|
// 15-Oct-2004
|
|
var d1 = Date.parseExact("10/15/2004", "M/d/yyyy");
|
|
|
|
// 15-Oct-2004
|
|
var d1 = Date.parse("15-Oct-2004", "M-ddd-yyyy");
|
|
|
|
// 15-Oct-2004
|
|
var d1 = Date.parse("2004.10.15", "yyyy.MM.dd");
|
|
|
|
// Multiple formats
|
|
var d1 = Date.parseExact("10/15/2004", ["M/d/yyyy", "MMMM d, yyyy"]);
|
|
</code></pre>
|
|
*
|
|
* @param {String} The string value to convert into a Date object [Required].
|
|
* @param {Object} The expected format {String} or an array of expected formats {Array} of the date string [Required].
|
|
* @return {Date} A Date object or null if the string cannot be converted into a Date.
|
|
*/
|
|
$D.parseExact = function (s, fx) {
|
|
return $D.getParseFunction(fx)(s);
|
|
};
|
|
}());
|