dojo.date.locale.parse
Usage
- info.
regexp
"$"
)
;
var
match = re.
exec
(
value
)
;
if
(
!
match
)
{
return
null
;
}
// null
var
widthList =
[
'abbr'
,
'wide'
,
'narrow'
]
;
var
result =
[
1970
,
0
,
1
,
0
,
0
,
0
,
0
]
;
// will get converted to a Date at the end
var
amPm =
""
;
var
valid = dojo.
every
(
match,
function
(
v, i
)
{
if
(
!
i
)
{
return
true
;
}
var
token=tokens
[
i
-1
]
;
var
l=token.
length
;
switch
(
token.
charAt
(
0
)
)
{
case
'y'
:
if
(
l
!
=
2
&&
options.
strict
)
{
//interpret year literally, so '5' would be 5 A.D.
result
[
0
]
= v;
}
else
{
if
(
v
<100){ v = Number(v); //choose century to apply, according to a sliding window //of 80 years before and 20 years after present year var year = '' + new Date().getFullYear(); var century = year.substring(0, 2) * 100; var cutoff = Math.min(Number(year.substring(2, 4)) + 20, 99); var num = (v < cutoff) ? century + v : century - 100 + v; result[0] = num; }else{ //we expected 2 digits and got more... if(options.strict){ return false; } //interpret literally, so '150' would be 150 A.D. //also tolerate '1950', if 'yyyy' input passed to 'yy' format result[0] = v; } } break; case 'M': if(l>2){ var months = bundle['months-format-' + widthList[l-3]].concat(); if(!options.strict){ //Tolerate abbreviating period in month part //Case-insensitive comparison v = v.replace(".","").toLowerCase(); months = dojo.map(months, function(s){ return s.replace(".","").toLowerCase(); } ); } v = dojo.indexOf(months, v); if(v == -1){ // console.debug("dojo.date.locale.parse: Could not parse month name: '" + v + "'."); return false; } }else{ v--; } result[1] = v; break; case 'E': case 'e': var days = bundle['days-format-' + widthList[l-3]].concat(); if(!options.strict){ //Case-insensitive comparison v = v.toLowerCase(); days = dojo.map(days, function(d){return d.toLowerCase();}); } v = dojo.indexOf(days, v); if(v == -1){ // console.debug("dojo.date.locale.parse: Could not parse weekday name: '" + v + "'."); return false; } //TODO: not sure what to actually do with this input, //in terms of setting something on the Date obj...? //without more context, can't affect the actual date //TODO: just validate? break; case 'D': result[1] = 0; // fallthrough... case 'd': result[2] = v; break; case 'a': //am/pm var am = options.am || bundle.am; var pm = options.pm || bundle.pm; if(!options.strict){ var period = /\./g; v = v.replace(period,'').toLowerCase(); am = am.replace(period,'').toLowerCase(); pm = pm.replace(period,'').toLowerCase(); } if(options.strict && v != am && v != pm){ // console.debug("dojo.date.locale.parse: Could not parse am/pm part."); return false; } // we might not have seen the hours field yet, so store the state and apply hour change later amPm = (v == pm) ? 'p' : (v == am) ? 'a' : ''; break; case 'K': //hour (1-24) if(v == 24){ v = 0; } // fallthrough... case 'h': //hour (1-12) case 'H': //hour (0-23) case 'k': //hour (0-11) //TODO: strict bounds checking, padding if(v > 23){ // console.debug("dojo.date.locale.parse: Illegal hours value"); return false; } //in the 12-hour case, adjusting for am/pm requires the 'a' part //which could come before or after the hour, so we will adjust later result[3] = v; break; case 'm': //minutes result[4] = v; break; case 's': //seconds result[5] = v; break; case 'S': //milliseconds result[6] = v; // break; // case 'w': //TODO var firstDay = 0; // default: //TODO: throw? // console.debug("dojo.date.locale.parse: unsupported pattern char=" + token.charAt(0)); } return true; }); var hours = +result[3]; if(amPm === 'p' && hours < 12){ result[3] = hours + 12; //e.g., 3pm -> 15 }else if(amPm === 'a' && hours == 12){ result[3] = 0; //12am -> 0 } //TODO: implement a getWeekday() method in order to test //validity of input strings containing 'EEE' or 'EEEE'... var dateObject = new Date(result[0], result[1], result[2], result[3], result[4], result[5], result[6]); // Date if(options.strict){ dateObject.setFullYear(result[0]); } // Check for overflow. The Date() constructor normalizes things like April 32nd... //TODO: why isn't this done for times as well? var allTokens = tokens.join(""); if(!valid || (allTokens.indexOf('M') != -1 && dateObject.getMonth() != result[1]) || (allTokens.indexOf('d') != -1 && dateObject.getDate() != result[2])){ return null; } return dateObject; // Dateparameter | type | description | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
value | String | A string representation of a date | ||||||||||||||||||||||||||||||
options | dojo.date.locale.__FormatOptions |
Optional. |
Create a Date object from a string using a known localized pattern. By default, this method parses looking for both date and time in the string. Formatting patterns are chosen appropriate to the locale. Different formatting lengths may be chosen, with "full" used by default. Custom patterns may be used or registered with translations using the dojo.date.locale.addCustomFormats method.
Formatting patterns are implemented using the syntax described at
unicode.org
When two digit years are used, a century is chosen according to a sliding
window of 80 years before and 20 years after present year, for both yy
and yyyy
patterns.
year