日期时间值在 DT:datatable() 中显示损坏

问题描述 投票:0回答:1

我有下面的数据框,我想将其显示为 DT::datatable()

library(DT)
library(lubridate)
eventex <- structure(list(registration_type = c("Start", "Stopp"),
                         timestamp = c("25.11.2022 13:19:42", 
                                       "31.10.2022 14:19:13")),
                         row.names = c(NA, -2L), 
                         class = c("tbl_df", "tbl", "data.frame"))

eventex$timestamp<-as.POSIXct( paste0(eventex$timestamp),
                               format="%d.%m.%Y %H:%M:%S")

datatable(eventex)

为什么会这样显示?

r datetime dt
1个回答
2
投票

人们对“正常”的定义可能会有所不同,尤其是在假设美国常用的模棱两可(且不合理)的惯例时。有一个 formatDate 函数可以应用于您的日期时间列。 (这些选项在 R 包中没有详细记录):

DT:::DateMethods
[1] "toDateString"       "toISOString"        "toLocaleDateString" "toLocaleString"     "toLocaleTimeString"
[6] "toString"           "toTimeString"       "toUTCString"   

datatable(eventex) %>% formatDate(~timestamp, "toLocaleString" )

更多信息和工作示例可以在这里找到:https://rstudio.github.io/DT/functions.html

© www.soinside.com 2019 - 2024. All rights reserved.