-4

I've a system generated date and time format. It looks something like this, "2017-04-12-02.29.25.000000" . I want to convert this format into a standard one so that my system can read this and later on I can convert it into minutes. Someone please help to provide a code in R.

Tyilo
  • 28,998
  • 40
  • 113
  • 198
  • "2017-04-12-02.29.25.000000" all of these are connected. Please tell a way to at least separate them out into "2017-04-12 02.29.25.000000" – Sarthak Bhardwajj Jun 11 '18 at 12:47
  • 3
    `as.POSIXct("2017-04-12-02.29.25.000000", "%Y-%m-%d-%H.%M.%S", origin="1970-01-01",tz="UTC")` – PKumar Jun 11 '18 at 12:49

1 Answers1

1

If you're unsure of the format, the guess_formats function in lubridate is pretty helpful:

w <- "2017-04-12-02.29.25.000000"
> lubridate::guess_formats(w, orders = 'YmdHMS')
              YOmdHMS                YmdHMS 
"%Y-%Om-%d-%H.%M.%OS"  "%Y-%m-%d-%H.%M.%OS"

orders is the format you want the function to investigate and it outputs the correct representation. If the second entry in the string is the day you can try YdmHMS.

The difference in the two formats in the output in the above example is based on formatting of the second entry (always with a leading zero or not). Trying the first format gives:

> as.POSIXct(w, format = "%Y-%Om-%d-%H.%M.%OS")
[1] "2017-04-12 02:29:25 EDT"

In the as.POSIXct call you may specify the timezone tz if required.

Gautam
  • 2,597
  • 1
  • 28
  • 51