new DateTime(stringDate, format);
new DateTime(stringDate, format,lang);
new DateTime(year, month, day, hours, minutes, seconds, milliseconds);
If you know the format of an input stringDate, you can use format to parse stringDate.
new DateTime("12-25-1995", "MM-DD-YYYY");
The parser ignores non-alphanumeric characters, so both of the following will return the same thing.
new DateTime("12-25-1995","MM-DD-YYYY");
new DateTime("12/25/1995", "MM-DD-YYYY");
Unless you specify a time zone offset, parsing a string will create a date in the current time zone. The parsing tokens are similar to the formatting tokens used in `DateTime#toString`.
Year, month, and day tokens
Input |
Example |
Description |
`YYYY` |
`2014` |
4 or 2 digit year |
`YY` |
`14` |
2 digit year |
`Y` |
`-25` |
Year with any number of digits and sign |
`Q` |
`1..4` |
Quarter of year. Sets month to first month in quarter. |
`M MM` |
`1..12` |
Month number |
`MMM MMMM` |
`Jan..December` |
Month name in locale set by `DateTime.locale()` |
`D DD` |
`1..31` |
Day of month |
`Do` |
`1st..31st` |
Day of month with ordinal |
`DDD DDDD` |
`1..365` |
Day of year |
`X` |
`1410715640.579` |
Unix timestamp |
`x` |
`1410715640579` |
Unix ms timestamp |
`Y` will match any number, signed or unsigned. It is useful for years that are not 4 digits or are before the common era. It can be used for any year.
Week year, week, and weekday tokens
For these, the lowercase tokens use the locale aware week start days, and the uppercase tokens use the
ISO week date start days.
Input |
Example |
Description |
`gggg` |
`2014` |
Locale 4 digit week year |
`gg` |
`14` |
Locale 2 digit week year |
`w ww` |
`1..53` |
Locale week of year |
`e` |
`0..6` |
Locale day of week |
`ddd dddd` |
`Mon...Sunday` |
Day name in locale set by `DateTime.Locale()` |
`GGGG` |
`2014` |
ISO 4 digit week year |
`GG` |
`14` |
ISO 2 digit week year |
`W WW` |
`1..53` |
ISO week of year |
`E` |
`1..7` |
ISO day of week |
Hour, minute, second, millisecond, and offset tokens
Input |
Example |
Description |
`H HH` |
`0..23` |
Hours (24 hour time) |
`h hh` |
`1..12` |
Hours (12 hour time used with `a A`.) |
`k kk` |
`1..24` |
Hours (24 hour time from 1 to 24) |
`a A` |
`am pm` |
Post or ante meridiem (Note the one character `a p` are also considered valid) |
`m mm` |
`0..59` |
Minutes |
`s ss` |
`0..59` |
Seconds |
`S SS SSS` |
`0..999` |
Fractional seconds |
`Z ZZ` |
`+12:00` |
Offset from UTC as `+-HH:mm`, `+-HHmm`, or `Z` |
fractional second tokens length 4 up to 9 can parse any number of digits, but will only consider the top 3 (milliseconds).
Use if you have the time printed with many fractional digits and want to consume the input.
Note that the number of `S` characters provided is only relevant when parsing in strict mode.
In standard mode, `S`, `SS`, `SSS`, `SSSS` are all equivalent, and interpreted as fractions of a second.
For example, `.12` is always 120 milliseconds, passing `SS` will not cause it to be
interpreted as 12 milliseconds.
Locale aware date and time formats are also available using `LT LTS L LL LLL LLLL`.
a locale key can be passed as the third parameter to `new DateTime()` and `DateTime.utc()`.
new DateTime('2012 juillet', 'YYYY MMM', 'fr');
new DateTime('2012 July', 'YYYY MMM', 'en');