📜  如何在 R 中分隔日期和时间?

📅  最后修改于: 2022-05-13 01:54:49.004000             🧑  作者: Mango

如何在 R 中分隔日期和时间?

在本文中,我们将在 R 编程语言中分离日期和时间。日期时间为日期时间格式(YYYY/MM/DD HH:MM:SS-年/月/日小时:分:秒)。

从时间戳中提取日期:我们将使用 as.Date()函数提取日期。

句法:

从时间戳中提取时间:我们可以使用 as.POSIXct()函数来做到这一点。要获得特定的时间小时格式,我们可以使用 format()函数

句法:

示例 1:

R
# create variable with one time stamp 
data ="2021/05/25 12:34:25"
                             
# extract date from the time stamp
print( as.Date(data))
  
# get time from date using format in 
# the form of hours and  minutes 
print( format(as.POSIXct(data), format = "%H:%M"))


R
# create variable with one time stamp 
data ="2021/05/25 12:34:25"
                             
# extract date from the time stamp
print( as.Date(data))
  
  
# get time from date using format in the 
# form of hours ,minutes  and seconds
print( format(as.POSIXct(data), format = "%H:%M:%S"))


R
# create data  with five time stamps 
data = c("2021/05/25 12:34:25",
         "2019/1/14 04:10:30",
         "2020/7/11 09:05:05",
         "2018/1/14 04:10:30",
         "2017/7/11 09:05:05")
                             
# extract date from the time stamp
print( as.Date(data))
  
# get time from date using format in the
# form of hours ,minutes  and seconds
print( format(as.POSIXct(data), format = "%H:%M:%S"))


输出:



示例 2:

电阻

# create variable with one time stamp 
data ="2021/05/25 12:34:25"
                             
# extract date from the time stamp
print( as.Date(data))
  
  
# get time from date using format in the 
# form of hours ,minutes  and seconds
print( format(as.POSIXct(data), format = "%H:%M:%S"))

输出:

示例 3:

电阻

# create data  with five time stamps 
data = c("2021/05/25 12:34:25",
         "2019/1/14 04:10:30",
         "2020/7/11 09:05:05",
         "2018/1/14 04:10:30",
         "2017/7/11 09:05:05")
                             
# extract date from the time stamp
print( as.Date(data))
  
# get time from date using format in the
# form of hours ,minutes  and seconds
print( format(as.POSIXct(data), format = "%H:%M:%S"))

输出: