【问题标题】:date format change with DT and shiny日期格式随 DT 和闪亮而变化
【发布时间】:2016-12-16 22:36:30
【问题描述】:

我的问题是当我在我的计算机和服务器上使用数据表时,formatDate 正在发生变化 我知道我正在使用method = 'toLocaleDateString',这可能不是好方法

在我的电脑上,它给了我想要的格式:

1 février 2000 

21 mars 2000

闪亮它给我:

01/02/2000

21/03/2000

本地计算机和服务器有Sys.timezone()

[1] "Europe/Paris"

我想这样做

a <-structure(list(timestamp = structure(c(949363200, 953596800, 
                                         961286400, 962582400,     965347200,     969667200), 
                                       class = c("POSIXct",  "POSIXt"), tzone = "UTC"), 
                 anoms = c(1, 1, 1, 1, 1, 2), syndrome = c("Acrosyndrome", 
                                                       "Acrosyndrome", "Acrosyndrome", "Acrosyndrome", "Acrosyndrome", 
                                                       "Acrosyndrome")), .Names = c("timestamp", "anoms", "syndrome"
                                                       ), row.names = c(NA, 6L), class = "data.frame")

datatable(a) %>% formatDate(  1, method = 'toLocaleDateString')
a

谢谢

【问题讨论】:

  • toDateString 可能会给你你想要的,除了星期几被添加到输出中;这在很大程度上取决于您的网络浏览器——不同的网络浏览器(和语言设置)可能会给您不同的输出。
  • toDateString 给我英文格式Tue Feb 01 2000 有没有办法在没有toLocaleDateString 或类似date.toLocaleDateString('fr-FR')developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 的情况下强制使用法语格式
  • 好主意。在 DT 的开发版中实现。

标签: r shiny dt


【解决方案1】:

使用 Github 上 DT (>= 0.2.2) 的development version,可以将额外的参数传递给日期转换方法,例如

datatable(a) %>%
  formatDate(1, method = 'toLocaleDateString', params = list('fr-FR'))

或更多参数:

datatable(a) %>% formatDate(
  1, method = 'toLocaleDateString',
  params = list('fr-FR',  list(year = 'numeric', month = 'long', day = 'numeric'))
)

【讨论】:

  • 您好@Yihui,感谢您的回答。更进一步,有没有办法使用这种方法获得 mm-YYYY 日期? (我已尝试在您上面的答案中设置 day=NULL,但这似乎不起作用。)再次感谢。
  • 有没有办法改变 Posix.ct 列中的日期格式。使用 `formatDate(1, method = 'toLocaleDateString', params = list('fr-FR'))` 可以让我更改日期格式,但时间部分会丢失。
猜你喜欢
  • 2020-02-03
  • 2017-12-31
  • 1970-01-01
  • 1970-01-01
  • 2016-10-25
  • 2020-09-18
  • 2014-05-15
  • 2018-01-15
  • 2018-12-03
相关资源
最近更新 更多