Formatting a date in R without leading zeros
Asked Answered
T

4

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.

Toinette answered 19/8, 2014 at 15:20 Comment(0)
G
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.

Ganges answered 19/8, 2014 at 15:25 Comment(5)
It's weird to me that there isn't just an option like %D or whatever that removes leading zeros.Samarskite
may as well make that fixed = TRUEHerbage
See RyanFrost's post below for a better solution that uses strftime's own formatting.Specious
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.Fibrilla
@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.Ganges
P
15

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"
Peavy answered 2/6, 2020 at 19:28 Comment(5)
I like the look of this sollution, however, it does not work for me (on Windows):> format(as.Date("2020-06-02"), "%Y, %m, %d") [1] "2020, 06, 02" > format(as.Date("2020-06-02"), "%Y, %#m, %#d") [1] "2020, #m, #d" I wonder why...Licking
This is by far the best solutionSpecious
this works great for mePredicative
it doesn't work on two Windows machines I have but I have the impression it's an issue related to the locale settings...Eversion
This doesn't work on my Windows machine.Ganges
F
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)

Fogdog answered 18/6, 2018 at 3:41 Comment(1)
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.Francis
J
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"))
Jeseniajesh answered 9/9, 2020 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.