R语言-时间日期函数

R语言时间日期函数

1. 返回当前日期时间,有两种方式:

Sys.time()
date()
  • 举例

      format(Sys.time(), "%a %b %d %X %Y %Z")
      #[1] "周五 五月 06 14:17:40 2016 CST"
      
      format(Sys.time(), "%H:%M:%OS3")
      #[1] "14:17:40.658"
    
      sysYear <- format(Sys.time(), '%Y')
      sysMonth <- format(Sys.time(), '%m')
      sysDay <- format(Sys.time(), '%d')
      sysHour <- format(Sys.time(), '%H')
      sysMinute <- format(Sys.time(), '%M')
      sysSecend <- format(Sys.time(), '%S')
    

2. 仅返回当前日期,使用函数Sys.Date()

  • 举例

      today <-Sys.Date()
      format(today, format="%B %d %Y")
      "June 20 2007"
    

3. 转换日期变量格式,有多种方式:

  • as.Date():

      此函数有多种使用方式。
      一、类似于函数format()和as.character(),返回给定的日期参数的特定格式,如as.Date(Sys.Date())的返回结果为"2011-08-09"。	
      二、形式as.Date(x,origin) 返回自参数origin(参数值为一日期)起第x天。如as.Date(2, origin="2011-08-09")的返回结果为"2011-08-11"。
      三、as.Date("2007-02-01")   #得到"2007-02-01",显示为字符串,但实际是用double存储的
      四、可以把定制的日期字符串转换为日期型,as.Date("2007年2月1日", "%Y年%m月%d日"), 结果 "2007-02-01"
    
  • format():

      一、如命令format(Sys.Date(), "%Y-%m-%d %w")的返回结果为"2011-08-09 2",其中2表示2011年8月9日为周二;
      二、若不指定返回的格式,则函数format()默认按照格式"%Y-%m-%d"返回,也就是说format(Sys.Date())和format(Sys.Date(), "%Y-%m-%d")的返回结果是相同的。
    
    format()参数的格式和涵义
    格 式 涵 义
    %Y 年份,以四位数字表示,2007
    %m 月份,以数字形式表示,从01到12
    %d 月份中当的天数,从01到31
    %b 月份,缩写,Feb
    %B 月份,完整的月份名,指英文,February
    %y 年份,以二位数字表示,07
    …… ……
  • as.character():

      其使用方法同format()相同,如:as.character(Sys.Date())。
    
  • 举例

      # convert date info in format 'mm/dd/yyyy'
      strDates <- c("01/05/1965", "08/16/1975")
      dates <- as.Date(strDates, "%m/%d/%Y")
    
      # convert dates to character data
      strDates <- as.character(dates)
    

4. 返回特定日期所对应的星期、月份、季度,分别使用函数:

weekdays()
months()
quarters()

5. 求两个日期之间的差,可通过函数julian或者diff.Date()实现

如求2015-09-10和2015-08-09两天之间相隔的天数,可以通过
julian(as.Date("2015-09-10"),origin=as.Date("2015-08-09"))[[1]]来求得。

#计算日期差

#由于日期内部是用double存储的天数,所以是可以相减的。
today <- Sys.Date()
gtd <- as.Date("2015-07-01")  
today - gtd
#Time difference of 310 days
#原来我到今天为止已经实践GTD有310天了。

#用difftime()函数可以计算相关的秒数、分钟数、小时数、天数、周数
difftime(today, gtd, units="weeks")  #还可以是“secs”, “mins”, “hours”, “days”
#Time difference of 44.28571 weeks

6. 生成时间序列向量,也有多种方式:

一、使用函数as.Date()。如as.Date(1:20, origin="2011-08-09")。

二、使用函数seq()。和seq()的一般使用方式的区别在于,梯度可以是"day", "week", "month" 或者"year",甚至是"3 months"等。如seq(as.Date("2000/1/1"), by="month", length.out=3)的返回结果为 "2000-01-01" "2000-02-01" "2000-03-01";函数seq(as.Date("2000/1/1"), as.Date("2003/1/1"), by="6 months")的返回结果为 "2000-01-01" "2000-07-01" "2001-01-01" "2001-07-01" "2002-01-01" "2002-07-01" "2003-01-01"。

7. 绘制图形,使用plot()即可

plot(x,y),其中参数x为日期时间类型的对象,y是与x相对应的数值。

8. 在R中日期实际是double类型,是从1970年1月1日以来的天数

typeof(Sys.Date())
#[1] "double"
as.double(as.Date("1970-01-01"))  #结果为0,是从1970年1月1日以来的天数。

9. POSIX类 ?POSIXct

The POSIXct class stores date/time values as the number of seconds since January 1, 1970, while the POSIXlt class stores them as a list with elements for second, minute, hour, day, month, and year, among others.
POSIXct 是以1970年1月1号开始的以秒进行存储,如果是负数,则是1970年以前;正数则是1970年以后。
POSIXlt 是以列表的形式存储:年、月、日、时、分、秒;
mydate = as.POSIXlt('2005-4-19 7:01:00')
## names(mydate)
## "sec"   "min"   "hour"  "mday"  "mon"   "year"  "wday"  "yday"  "isdst"
mydate$mday
## 19
默认情况下,日期之前是以/或者-进行分隔,而时间则以:进行分隔;
输入的标准格式为:日期 时间(日期与时间中间有空隔隔开)
时间的标准格式为:时:分 或者 时:分:秒;
如果输入的格式不是标准格式,则同样需要使用strptime函数,利用format来进行指定。

10. R语言记录程序运行的时间

# 可以根据不同的需求调整最后的输出格式
# Since you only want the H:M:S, we can ignore the date...
# but you have to be careful about time-zone issues
AllTimeUsingFun <- function(start_time) {
  start_time <- as.POSIXct(start_time)
  dt <- difftime(Sys.time(), start_time, units="secs")
  format(.POSIXct(dt, tz="GMT"), "%H:%M:%S")
}

#how to use:
##start
start_time <- Sys.time()
##program
  Sys.sleep(5)
##end
AllTimeUsingFun(start_time)
posted @ 2016-05-06 15:40  银河统计  阅读(8072)  评论(0编辑  收藏  举报