_.parseDate()
Syntax
Parameters
- format
- the format that describes the output
- dateString
- the string-formatted date to parse
- (return value)
- the Date; undefined if parsing failed; or null if the string was empty and the date format is flagged as optional ('?' at the beginning)
Description
Parses the given string as Date using the given format. The format specifies which date component is expected where in the string. It can also be used to specify the timezone of the input string, and it may specify whether an empty string (including strings containing only whitespace) is allowed.
In the date format there are a number of reserved characters that are used as placeholders of date components. If you put a single
character in the format, this will match numbers of any length. If you have two or more of the same character, this is recognized as
fixed-length string.
Some placeholders, such as month names, support translations. To parse dates in other languages, you can specify a comma-separated
list of translations in brackets following the placeholder.
The following placeholder characters are supported.
| Character | Description |
|---|---|
| y | Year (4 digits) |
| Y | Year (2 digits, 2000-based) |
| M | Month (1-12) |
| n | Month as short name ('Jan', 'Feb'...). Supports translations. |
| N | Month as long name ('January', 'February'...). Supports translations. |
| d | Day of month (1-31) |
| m | Minutes (0-59) |
| H | Hours in 24h format (0-23) |
| h | Hours in 12h format (1-12) |
| K | Hours in 0-based 12h format (0-11) |
| k | Hours in 1-based 24h format (1-24) |
| s | Seconds (0-59) |
| S | Milliseconds (0-999) |
| a | Either 'am' or 'pm'. Supports translations. |
| w | Day of week as short name ('Sun', 'Mon'...). Supports translations. |
| W | Day of week as long name ('Sunday', 'Monday'...). Supports translations. |
| z | Timezone offset, e.g. '+0700' |
parseDate() also supports parsing a date in a different timezone. You only need to put the timezone in brackets at the front of the format, e.g. '[+0100]'.
All other characters are expected to be identical in format and input string, with the exception of whitespace. Each whitespace character in the format can match any number of other whitespace characters in the input string, but at least one.
Any components that are not in the format will be set to 0. For example, if your format has only date, month and day, the resulting date will be at midnight.
Example
Parsing dates in various formats.
var v1 = _.parseDate('y-M-d', '2013-7-9');
var v2 = _.parseDate('?yyyyMMdd', '20130709');
var v3 = _.parseDate('?yyyyMMdd', ' '); // returns null
var v4 = _.parseDate('yyyy-MM-ddTHH:mm:ss.SS z', '2013-07-09T23:07:38.472 +0700');
var v5 = _.parseDate('MM/dd/YY h:mm:ss a', '07/09/13 11:07:38 pm');
var v6 = _.parseDate('dd.MM.yyyy HH:mm:ss', '09.07.2013 23:07:38');
var v7 = _.parseDate('W, N d y', 'Tuesday, July 9 2013');
var v8 = _.parseDate('d.N[Januar,Februar,Maerz,April,Mai,Juni,Juli,'+
'August,September,Oktober,November,Dezember] y', '9. Juli 2013'); // parsing german
var v9 = _.parseDate('[+0100]yyyy-MM-dd h:mm a', '2013-07-09 5:07 pm'); // different timezone:
See also..
_.formatValue()can format dates using the same syntax.
Comments
comments powered by DisqusFunctions
- $() Web
- list.length Web, Util
- $$() Web
- $.getCookie()
- $.loop() Web
- $.off() Web
- $.parseJSON() Web
- $.ready() Web
- $.request() Web
- $.setCookie()
- $.toJSON() Web
- $.wait()
- .add() Web
- .addAfter() Web
- .addBefore() Web
- .addFront() Web
- .animate() Web
- .array() Util
- .call() Util
- .clone() Web
- .collect() Web, Util
- .contains() Util
- .dial() Web
- .each() Web, Util
- .endsWith() Util
- .equals() Util
- .fill() Web
- .filter() Web, Util
- .find() Web, Util
- .findLast() Web, Util
- .get() Web
- .hide() Web
- .ht()
- .intersection() Util
- .is() Web
- .join() Util
- .map() Util
- .merge() Util
- .next() Web
- .not() Web
- .offset()
- .on() Web
- .onChange() Web
- .onClick() Web
- .onFocus() Web
- .onOver() Web
- .only()
- .per() Util
- .reduce() Util
- .remove() Web
- .replace() Web
- .reverse() Util
- .select() Web
- .set() Web
- .show() Web
- .sort() Util
- .startsWith() Util
- .sub() Web, Util
- .text() Web
- .toObject() Util
- .toggle() Web
- .trav() Web
- .trigger() Web
- .uniq() Util
- .unite() Util
- .up() Web
- .values() Web
- EE() Web
- HTML() Web
- M Web, Util
- MINI.getter Web
- MINI.setter Web
- Minified Lists Web, Util
- Promise Web, Util
- _() Util
- _.bind() Util
- _.copyObj() Util
- _.dateAdd() Util
- _.dateClone() Util
- _.dateDiff() Util
- _.dateMidnight() Util
- _.eachObj() Util
- _.escapeHtml() Util
- _.escapeRegExp() Util
- _.extend() Util
- _.filterObj() Util
- _.format() Util
- _.formatHtml() Util
- _.formatValue() Util
- _.isBool() Util
- _.isDate() Util
- _.isEmpty() Util
- _.isFunction() Util
- _.isList() Util
- _.isNumber() Util
- _.isObject() Util
- _.isString() Util
- _.isValue() Util
- _.keys() Util
- _.mapObj() Util
- _.pad() Util
- _.parseDate() Util
- _.parseNumber() Util
- _.partial() Util
- _.promise()
- _.range()
- _.template() Util
- _.toString() Util
- _.trim() Util
- _.values() Util
- define() Web, Util
- promise.always()
- promise.error() Web, Util
- promise.fire()
- promise.stop()
- promise.then() Web
- require() Web, Util
- How to...