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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM