0

An argument for an API requires me to format time as YYYYMMDDHHMM, i.e. 3 March 2019 14:30 would be "201903031430". I'm trying to do some operations beforehand and then convert the result into that string format, but I haven't come up with anything succinct. For example, using lubridate:

library(lubridate)

paste0(year(now()),month(now()),day(now()),hour(now()),minute(now()))

> [1] "2020122105"

Not only looks horrible, but also does not pad zeroes for days and hours as required. I can tweak the relevant ones further for zero-padding, but I was wondering if there is a more elegant way of reversing the typical string-to-datetime conversion.

Fons MA
  • 1,142
  • 1
  • 12
  • 21
  • 4
    Wouldn't this be easier with `format` `format(now(), "%Y%m%d%H%M")` – akrun Jan 21 '20 at 23:17
  • 3
    Base R: `strftime(Sys.time(), "%Y%m%d%H%M")` – Dirk Eddelbuettel Jan 21 '20 at 23:17
  • Thank you! I knew there had to be a much easier way... I need to consider/look into base R a bit more. If either of you would like to put this as an answer I can accept it. – Fons MA Jan 21 '20 at 23:27
  • 2
    I believe this is a duplicate of [Current time in ISO 8601 format](https://stackoverflow.com/questions/29517896/current-time-in-iso-8601-format) or [Extracting time from POSIXct](https://stackoverflow.com/questions/9839343/extracting-time-from-posixct/9839566#9839566) – wibeasley Jan 22 '20 at 06:57
  • @wibeasley, I think the problem is that those are a lot more specific and did not show up when I did a DDG search or when writing my question (which I did pretty much expecting something to have already been asked). Happy for this to be closed, but I thought I'd mention that. – Fons MA Jan 22 '20 at 09:32
  • @FonsMA, sorry they didn't show up. If you're happy that these posts are similar enough to someone searching for this, it's probably best if you close it as a duplicate. – wibeasley Jan 22 '20 at 15:28

0 Answers0