日期和时间
1.获取系统当前日期和时间Sys.Date() and Sys.time()
2.日期格式:%Y: 4-digit year (1982);%y: 2-digit year (82);%m: 2-digit month (01);%d: 2-digit day of the month (13)
%A: weekday (Wednesday);%a: abbreviated weekday (Wed);%B: month (January);%b: abbreviated month (Jan)
1)默认格式:"%Y-%m-%d" or "%Y/%m/%d"
2)创建日期:as.Date()
例1:as.Date("1982-01-13")
as.Date("Jan-13-82", format = "%b-%d-%y") tip:"Jan-13-82"因为是字符串所以要加引号,如果是向量则不需要引号。
as.Date("13 January, 1982", format = "%d %B, %Y")
3)改变日期格式:format()
例2:today <- Sys.Date()
format(Sys.Date(), format = "%d %B, %Y")
format(Sys.Date(), format = "Today is a %A!")
4)日期间计算
例:today <- Sys.Date()
today + 1;today - 1;as.Date("2015-03-12") - as.Date("2015-02-27")
3.时间格式:%H: hours as a decimal number (00-23)一小时内;%I: hours as a decimal number (01-12)超过一小时;%M: minutes as a decimal number
%S: seconds as a decimal number;%T: shorthand notation for the typical format %H:%M:%S;%p: AM/PM indicator
1)默认格式:%Y-%m-%d %H:%M:%S
2)创建时间:as.POSIXct()
*diff()函数:将它的输入向量移动1个下标,然后计算这个移动的向量和原始向量之间的差。用这种方法,它计算了连续向量元素之间的差
3)时间的计算
例:now <- Sys.time()
now + 3600 # add an hour ;now - 3600 * 24 # subtract a day
网友评论