Date formatting in R without leading zeros

Is there a way to use the format function for a date object, in particular an object of class POSIXlt , POSIXct or Date , with the format %Y, %m, %d , so that leading zeros are removed from each of these three fields?

For example, I would like format(as.Date("1998-09-02"), "%Y, %m, %d") return 1998, 9, 2 , and not 1998, 09, 02 .

+7
date r
source share
1 answer

Just remove the leading zeros at the end:

 > gsub(" 0", " ", format(as.Date("1998-09-02"), "%Y, %m, %d")) [1] "1998, 9, 2" 

Use % e to get the leading space instead of the leading zero.

+8
source share

All Articles