當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R語言 strptime()用法及代碼示例


R 語言中的 strptime() 函數用於使用給定的模板解析給定的日期和時間表示。

用法: strptime(x, format, tz = “”)
參數: 
x: given representation of date and time 
y: given template in which parsing is done 
tz: a character string specifying the time zone to be used for the conversion 
 

範例1:

Python3


# R program to illustrate
# strptime function
 
# Specifying a time
x <- "13:15:17"
 
# Calling strptime() function
# over specified time and template
y <- strptime(x, "% H:% M:% S")
 
# Getting the current date and
# given time into specified template
y

輸出:



[1] "2020-06-17 13:15:17 UTC"

範例2:

Python3


# R program to illustrate
# strptime function
 
# Specifying a date and time
x <- "10-02-2020 05:05:06 AM"
 
# Calling strptime() function
# over specified date and time, template
# and time zone
y <- strptime(x, "% d-% m-% Y % I:% M:% S", "GMT")
 
# Getting parsed date and time
y

輸出:

[1] "2020-02-10 05:05:06 GMT"

相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Parsing Date and Time in R Programming – strptime() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。