jQuery: The Write Less, Do More JavaScript Library

UI/Datepicker/$.datepicker.parseDate

From jQuery JavaScript Library

Jump to: navigation, search


« Back to UI/Datepicker

$.datepicker.parseDate( format, value, [settings] )

Extract a date from a string value with a specified format.
The format can be combinations of the following:
  • d - day of month (no leading zero)
  • dd - day of month (two digit)
  • D - day name short
  • DD - day name long
  • m - month of year (no leading zero)
  • mm - month of year (two digit)
  • M - month name short
  • MM - month name long
  • y - year (two digit)
  • yy - year (four digit)
  • '...' - literal text
  • '' - single quote
  • anything else - literal text

A number of exceptions may be thrown:

  • 'Invalid arguments' if either format or value is null
  • 'Missing number at position nn' if format indicated a numeric value that is not then found
  • 'Unknown name at position nn' if format indicated day or month name that is not then found
  • 'Unexpected literal at position nn' if format indicated a literal value that is not then found
  • 'Invalid date' if the date is invalid, such as '31/02/2007'
Since v3.1.
Arguments:

formatString
The format to use in extracting the date.
valueString
The date value to be examined.
settings (Optional)Object
Additional parameters for parsing the date. All are optional and default to the datepicker default, which is usually standard English.

Since v3.4 - prior to this these settings were separate parameters.
Attributes include:
shortYearCutoff (Number) - the value (0-99) beyond which years are considered to be in the previous century. Used in conjunction with the 'y' format option. Defaults to the datepicker default, which is usually 10 years in the future.
dayNamesShort (String[7]) - the short names for the days of the week, starting at Sunday.
dayNames (String[7]) - the long names for the days of the week, starting at Sunday
monthNamesShort (String[12]) - the short names for the months of the year.
monthNames (String[12]) - the long names for the months of the year.


Examples:
Extract a date in ISO format.

$.datepicker.parseDate('yy-mm-dd', '2007-01-26');

Extract a date in expanded French format.

$.datepicker.parseDate('DD, MM d, yy', 'Samedi, Juillet 14, 2007', {shortYearCutoff: 20, dayNamesShort: $.datepicker.regional['fr'].dayNamesShort, dayNames: $.datepicker.regional['fr'].dayNames, monthNamesShort: $.datepicker.regional['fr'].monthNamesShort, monthNames: $.datepicker.regional['fr'].monthNames});

NameType