Formatting a date in R without leading zeros

前端 未结 4 1592
野性不改
野性不改 2020-12-07 00:33

Is there a way to use the format function on a date object, specifically an object of class POSIXlt, POSIXct, or Date, wi

4条回答
  •  一向
    一向 (楼主)
    2020-12-07 00:57

    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"
    

提交回复
热议问题