6

my problem is when i use datatable on my computer and on the server formatDate is changing i know i'm using method = 'toLocaleDateString' maybe it's not the good method

on my computer it give me the format i want :

1 février 2000 

21 mars 2000

on shiny it give me :

01/02/2000

21/03/2000

local computer and server have Sys.timezone()

[1] "Europe/Paris"

im trying to do it like this

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

Thank you

Carl
  • 5,569
  • 6
  • 39
  • 74
s.brunel
  • 1,003
  • 10
  • 24
  • `toDateString` might give you what you want, except that the day of the week is added to the output; this is highly dependent on your web browser -- different web browsers (and language settings) may give you different output. – Yihui Xie Aug 10 '16 at 19:10
  • 1
    `toDateString` give me the english format `Tue Feb 01 2000` is there a way to force the french one without `toLocaleDateString` or something like `date.toLocaleDateString('fr-FR')` from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString#Using_toLocaleDateString() – s.brunel Aug 11 '16 at 06:49
  • Good idea. Implemented in the dev version of DT. – Yihui Xie Aug 11 '16 at 22:09

1 Answers1

8

With the development version of DT (>= 0.2.2) on Github, you can pass additional parameters to the date conversion method, e.g.

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

Or more parameters:

datatable(a) %>% formatDate(
  1, method = 'toLocaleDateString',
  params = list('fr-FR',  list(year = 'numeric', month = 'long', day = 'numeric'))
)
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • 6
    Hi @Yihui and thank you for this answer. Taking this further, is there a way to have a mm-YYYY date using this approach? (I have tried to set day=NULL in your answer above, but that doesn't seem to work.) Thanks again. – p0bs May 24 '17 at 12:39
  • Is there a way to change the date format in a Posix.ct column. Using ` formatDate(1, method = 'toLocaleDateString', params = list('fr-FR'))` gets me the change in date format but the time component is lost. – Lazarus Thurston Jul 10 '21 at 08:31