Learn R Programming

stringi (version 1.8.4)

stri_datetime_format: Date and Time Formatting and Parsing

Description

These functions convert a given date/time object to a character vector, or vice versa.

Usage

stri_datetime_format(
  time,
  format = "uuuu-MM-dd HH:mm:ss",
  tz = NULL,
  locale = NULL
)

stri_datetime_parse( str, format = "uuuu-MM-dd HH:mm:ss", lenient = FALSE, tz = NULL, locale = NULL )

Value

stri_datetime_format returns a character vector.

stri_datetime_parse returns an object of class POSIXct.

Arguments

time

an object of class POSIXct with date-time data to be formatted (as.POSIXct will be called on character vectors and objects of class POSIXlt, Date, and factor)

format

character vector, see Details; see also stri_datetime_fstr

tz

NULL or '' for the default time zone or a single string with a timezone identifier, see stri_timezone_get and stri_timezone_list

locale

NULL or '' for the default locale, or a single string with locale identifier; a non-Gregorian calendar may be specified by setting the @calendar=name keyword

str

character vector with strings to be parsed

lenient

single logical value; should date/time parsing be lenient?

Author

Marek Gagolewski and other contributors

Details

Vectorized over format and time or str.

When parsing strings, unspecified date-time fields (e.g., seconds where only hours and minutes are given) are based on today's midnight in the local time zone (for compatibility with strptime).

By default, stri_datetime_format (for compatibility with the strftime function) formats a date/time object using the current default time zone.

format may be one of DT_STYLE or DT_relative_STYLE, where DT is equal to date, time, or datetime, and STYLE is equal to full, long, medium, or short. This gives a locale-dependent date and/or time format. Note that currently ICU does not support relative time formats, thus this flag is currently ignored in such a context.

Otherwise, format is a pattern: a string where specific sequences of characters are replaced with date/time data from a calendar when formatting or used to generate data for a calendar when parsing. For example, y stands for 'year'. Characters may be used multiple times: yy might produce 99, whereas yyyy yields 1999. For most numerical fields, the number of characters specifies the field width. For example, if h is the hour, h might produce 5, but hh yields 05. For some characters, the count specifies whether an abbreviated or full form should be used.

Two single quotes represent a literal single quote, either inside or outside single quotes. Text within single quotes is not interpreted in any way (except for two adjacent single quotes). Otherwise, all ASCII letters from a to z and A to Z are reserved as syntax characters, and require quoting if they are to represent literal characters. In addition, certain ASCII punctuation characters may become available in the future (e.g., : being interpreted as the time separator and / as a date separator, and replaced by respective locale-sensitive characters in display).

SymbolMeaningExample(s)Output
Gera designatorG, GG, or GGGAD
GGGGAnno Domini
GGGGGA
yyearyy96
y or yyyy1996
uextended yearu4601
Ucyclic year name, as in Chinese lunar calendarU
rrelated Gregorian yearr1996
QquarterQ or QQ02
QQQQ2
QQQQ2nd quarter
QQQQQ2
qStand Alone quarterq or qq02
qqqQ2
qqqq2nd quarter
qqqqq2
Mmonth in yearM or MM09
MMMSep
MMMMSeptember
MMMMMS
LStand Alone month in yearL or LL09
LLLSep
LLLLSeptember
LLLLLS
wweek of yearw or ww27
Wweek of monthW2
dday in monthd2
dd02
Dday of yearD189
Fday of week in monthF2 (2nd Wed in July)
gmodified Julian dayg2451334
Eday of weekE, EE, or EEETue
EEEETuesday
EEEEET
EEEEEETu
elocal day of weeke or ee2
example: if Monday is 1st day, Tuesday is 2nd )eeeTue
eeeeTuesday
eeeeeT
eeeeeeTu
cStand Alone local day of weekc or cc2
cccTue
ccccTuesday
cccccT
ccccccTu
aam/pm markerapm
hhour in am/pm (1~12)h7
hh07
Hhour in day (0~23)H0
HH00
khour in day (1~24)k24
kk24
Khour in am/pm (0~11)K0
KK00
mminute in hourm4
mm04
ssecond in minutes5
ss05
Sfractional second - truncates (like other time fields)S2
to the count of letters when formatting. AppendsSS23
zeros if more than 3 letters specified. Truncates atSSS235
three significant digits when parsing.SSSS2350
Amilliseconds in dayA61201235
zTime Zone: specific non-locationz, zz, or zzzPDT
zzzzPacific Daylight Time
ZTime Zone: ISO8601 basic hms? / RFC 822Z, ZZ, or ZZZ-0800
Time Zone: long localized GMT (=OOOO)ZZZZGMT-08:00
Time Zone: ISO8601 extended hms? (=XXXXX)ZZZZZ-08:00, -07:52:58, Z
OTime Zone: short localized GMTOGMT-8
Time Zone: long localized GMT (=ZZZZ)OOOOGMT-08:00
vTime Zone: generic non-locationvPT
(falls back first to VVVV)vvvvPacific Time or Los Angeles Time
VTime Zone: short time zone IDVuslax
Time Zone: long time zone IDVVAmerica/Los_Angeles
Time Zone: time zone exemplar cityVVVLos Angeles
Time Zone: generic location (falls back to OOOO)VVVVLos Angeles Time
XTime Zone: ISO8601 basic hm?, with Z for 0X-08, +0530, Z
Time Zone: ISO8601 basic hm, with ZXX-0800, Z
Time Zone: ISO8601 extended hm, with ZXXX-08:00, Z
Time Zone: ISO8601 basic hms?, with ZXXXX-0800, -075258, Z
Time Zone: ISO8601 extended hms?, with ZXXXXX-08:00, -07:52:58, Z
xTime Zone: ISO8601 basic hm?, without Z for 0x-08, +0530
Time Zone: ISO8601 basic hm, without Zxx-0800
Time Zone: ISO8601 extended hm, without Zxxx-08:00
Time Zone: ISO8601 basic hms?, without Zxxxx-0800, -075258
Time Zone: ISO8601 extended hms?, without Zxxxxx-08:00, -07:52:58
'escape for text'(nothing)
' 'two single quotes produce one' ''

Note that any characters in the pattern that are not in the ranges of [a-z] and [A-Z] will be treated as quoted text. For instance, characters like :, ., (a space), # and @ will appear in the resulting time text even if they are not enclosed within single quotes. The single quote is used to ``escape'' the letters. Two single quotes in a row, inside or outside a quoted sequence, represent a ``real'' single quote.

A few examples:

Example PatternResult
yyyy.MM.dd 'at' HH:mm:ss zzz2015.12.31 at 23:59:59 GMT+1
EEE, MMM d, ''yyczw., gru 31, '15
h:mm a11:59 PM
hh 'o''clock' a, zzzz11 o'clock PM, GMT+01:00
K:mm a, z11:59 PM, GMT+1
yyyyy.MMMM.dd GGG hh:mm aaa2015.grudnia.31 n.e. 11:59 PM
uuuu-MM-dd'T'HH:mm:ssZ2015-12-31T23:59:59+0100 (the ISO 8601 guideline)

References

Formatting Dates and Times -- ICU User Guide, https://unicode-org.github.io/icu/userguide/format_parse/datetime/

See Also

The official online manual of stringi at https://stringi.gagolewski.com/

Gagolewski M., stringi: Fast and portable character string processing in R, Journal of Statistical Software 103(2), 2022, 1-59, tools:::Rd_expr_doi("10.18637/jss.v103.i02")

Other datetime: stri_datetime_add(), stri_datetime_create(), stri_datetime_fields(), stri_datetime_fstr(), stri_datetime_now(), stri_datetime_symbols(), stri_timezone_get(), stri_timezone_info(), stri_timezone_list()

Examples

Run this code
x <- c('2015-02-28', '2015-02-29')
stri_datetime_parse(x, 'yyyy-MM-dd')
stri_datetime_parse(x, 'yyyy-MM-dd', lenient=TRUE)
stri_datetime_parse(x %s+% " 17:13", "yyyy-MM-dd HH:mm")
stri_datetime_parse('19 lipca 2015', 'date_long', locale='pl_PL')
stri_datetime_format(stri_datetime_now(), 'datetime_relative_medium')

Run the code above in your browser using DataLab