31

Is there a way to use the format function on a date object, specifically an object of class POSIXlt, POSIXct, or Date, with the format %Y, %m, %d such that leading zeros are stripped from each of those 3 fields?

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

Jon Claus
  • 2,862
  • 4
  • 22
  • 33

4 Answers4

22

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 obtain a leading space instead of a leading zero.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • 25
    It's weird to me that there isn't just an option like `%D` or whatever that removes leading zeros. – Tom May 21 '15 at 01:43
  • 1
    may as well make that `fixed = TRUE` – MichaelChirico Jun 07 '17 at 15:40
  • 1
    See RyanFrost's post [below](https://stackoverflow.com/a/62159914/5851018) for a better solution that uses `strftime`'s own formatting. – Fons MA Aug 23 '21 at 12:19
  • This requires there to be a blank space before the 0. If you remove that part of the token being matched then it would mess up every date that ends in 0 like 10, 20, 30. I do not recommend this approach. – Hack-R Dec 01 '22 at 19:33
  • 1
    @Fons MA, @Hack=R., `%#d` and `%-d` are platform specific and do not work on my Windows machine. The solution in this answer works on all platforms. – G. Grothendieck Dec 01 '22 at 20:24
13

You can do this with a simple change to your strftime format string. However, it depends on your platform (Unix or Windows).

Unix

Insert a minus sign (-) before each term you'd like to remove leading zeros from:

format(as.Date("2020-06-02"), "%Y, %-m, %-d")
[1] "2020, 6, 2"

Windows

Insert a pound sign (#) before each desired term:

format(as.Date("2020-06-02"), "%Y, %#m, %#d")
[1] "2020, 6, 2"
Community
  • 1
  • 1
RyanFrost
  • 1,400
  • 7
  • 17
11

I have discovered a workaround by using year(), month() and day() function of {lubridate} package. With the help of glue::glue(), it is easy to do it as following:

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(glue)
dt <- "1998-09-02"
glue("{year(dt)}, {month(dt)}, {day(dt)}")
#> 1998, 9, 2

Created on 2021-04-19 by the reprex package (v2.0.0)

Edit on 2023-03-02:

After {tidyverse} 2.0.0, {lubridate} is attached after attaching {tidyverse}, so there is no need to attach {lubridate} now:

library(tidyverse)
dt <- "1998-09-02"
str_glue("{year(dt)}, {month(dt)}, {day(dt)}")
#> 1998, 9, 2

Created on 2023-03-02 with reprex v2.0.2

If {tidyverse} is used (suggested by @banbh), then str_glue() can be used:

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
dt <- "1998-09-02"
str_glue("{year(dt)}, {month(dt)}, {day(dt)}")
#> 1998, 9, 2

Created on 2021-04-19 by the reprex package (v2.0.0)

Liang Zhang
  • 753
  • 7
  • 20
  • 2
    Also, if you've loaded the `tidyverse` package (or just `stringr`) then you can use `str_glue()` instead of `glue()` and avoiding loading the `glue` package. – banbh Sep 25 '20 at 15:05
2

A more general solution using gsub, to remove leading zeros from the day or month digits produced by %m or %d. This deletes any zero that is not preceded by a digit:

gsub("(\\D)0", "\\1", format(as.Date("1998-09-02"), "%Y, %m, %d"))
user3799203
  • 444
  • 1
  • 4
  • 15