【862】as.Date in R programming

发布时间 2023-07-20 12:45:48作者: McDelfino

ref: R语言——日期时间处理

ref: as.Date: Date Conversion Functions to and from Character

ref: Date Formats in R


as.Date()it can change a normal string into a date format.

format()It can extract the specific date format from a date string.

Example:

> d = "2020-02-21"
> format(as.Date(d), format="%B, %Y")
[1] "February, 2020"
> format(as.Date(d), format="%d %B, %Y")
[1] "21 February, 2020"
> format(as.Date(d), format="%d %B, %Y, %A")
[1] "21 February, 2020, Friday"

Date Formats

Conversion specification Description Example
%a Abbreviated weekday Sun, Thu
%A Full weekday Sunday, Thursday
%b or %h Abbreviated month May, Jul
%B Full month May, July
%d Day of the month
01-31
27, 07
%j Day of the year
001-366
148, 188
%m Month
01-12
05, 07
%U Week
01-53
with Sunday as first day of the week
22, 27
%w Weekday
0-6
Sunday is 0
0, 4
%W Week
00-53
with Monday as first day of the week
21, 27
%x Date, locale-specific  
%y Year without century
00-99
84, 05
%Y Year with century
on input:
00 to 68 prefixed by 20
69 to 99 prefixed by 19
1984, 2005
%C Century 19, 20
%D Date formatted %m/%d/%y 05/27/84, 07/07/05
%u Weekday
1-7
Monday is 1
7, 4
     
%n Newline on output or
Arbitrary whitespace on input
 
%t Tab on output or
Arbitrary whitespace on input