当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。