Members
(static) PARSE_RECIPES
Recipes for parsing the date
A recipe consists of a regular expression fragment (string) and a parsing function. This object maps parse tokens to factory functions that take a format configuration object and returns a recipe.
The recipes are represented by an object with re
and fn
keys. The re
key is a string that represents a regular expression fragment that will
match any valid data for the token. The fn
function takes the string
matched by the regular expression fragment, and an object that tracks the
progress of various values that were parsed. The function should examine the
string and update the progress object.
To add a recipe for a new token, or override a recipe for an existing one,
we simply assign to the corresponding key on the parse.PARSE_RECIPES
object.
Example
parse.PARSE_RECIPES['%R'] = function (conf) {
return {
re: '....',
fn: function (s, parsedValues) {
// do something with `s` and update `parsedValues`
},
};
};
Methods
(static) strptime(s, formatString, configopt)
Parse a date/time string according to a format string
The return value is a Date
object or null
if the input does not match
the format string.
The Date
object is always in the local time zone, but if time zone offset
appears in the string, it will be taken into account and the resulting
object adjusted accordingly.
The format string is an arbitrary string that contains format sequences
%b
- Short month name (e.g., 'Jan', 'Feb'...).%B
- Full month name (e.g., 'January', 'February'...).%d
- Zero-padded date (e.g, 02, 31...).%D
- Non-zero-padded date (e.g., 2, 31...).%H
- Zero-padded hour in 24-hour format (e.g., 8, 13, 0...).%i
- Non-zero-padded hour in 12-hour format (e.g., 8, 1, 12...).%I
- Zero-padded hour in 12-hour format (e.g., 08, 01, 12...).%m
- Zero-padded month (e.g., 01, 02...).%M
- Zero-padded minutes (e.g., 01, 12, 59...).%n
- Non-zero-padded month (e.g., 1, 2...).%N
- Non-zero-padded minutes (e.g., 1, 12, 59).%p
- AM/PM (a.m. and p.m.).%s
- Non-zero-padded seconds (e.g., 1, 2, 50...).%S
- Zero-padded seconds (e.g., 01, 02, 50...).%r
- Milliseconds (e.g., 1, 24, 500...).%y
- Zero-padded year without the century part (e.g., 01, 13, 99...).%Y
- Full year (e.g., 2001, 2013, 2099...).%z
- UTC offset in +HHMM or -HHMM format or 'Z' (e.g., +1000, -0200).%%
- literal percent character.
The %z
token behaves slightly differently when parsing date and time
strings. In addition to formats that strftime outputs, it also supports
'Z', which allows parsing of ISO timestamps.
Example
let s1 = '2019 Jan 12 4:55 p.m.';
let d1 = parse.strptime(s, '%Y %b %D %i:%M %p');
// => new Date(2019, 0, 12, 16, 55)
// With customized AM/PM
let s2 = '18/March/2019 07:22am';
let d2 = parse.strptime(s, '%d/%B/%Y %I:%M%p', {
AM: 'am',
PM: 'pm',
});
// => new Date(2019, 2, 18, 7, 22)
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
s |
string | Formatted date/time string |
||
formatString |
string | The expected format of the |
||
config |
object |
<optional> |
DEFAULT_CONFIG
|
Format configuration |