0

Current situation:

mydate <- "14:45"

class(mydate)

The current class of this value is a character. I would like to convert it into a POSIXlt format.

I tried the strptime() function but it unfortunately adds the full date to my hours when I actually only need Hours:Minutes

mydate <- strptime(mydate, format = "%H:%M")

What can I do to get a POSIXlt format uniquely containing hours and minutes ?

Thanks in advance for your returns !

  • 1
    `POSIXlt` always contains the date as well. You can convert it back to character without the date later on but I don't see the point. – JBGruber Dec 11 '19 at 13:42
  • Thnx a lot ! I thought it was possible to get that format only containing hours and minutes – Charliebr0wn Dec 11 '19 at 13:47

2 Answers2

2

POSIXlt and POSIXct always contain date and time. You can use chron times class to represent times less than 24:00:00.

library(chron)

tt <- times(paste(mydate, "00", sep = ":"))
tt
## [1] 14:45:00

times class objects are represented internally as a fraction of a day so, for example, adding 1/24 will add an hour.

tt + 1/24 # add one hour
## [1] 15:45:00
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
0

For me it works like this:

test <- "2016-04-10T12:21:25.4278624"
z <- as.POSIXct(test,format="%Y-%m-%dT%H:%M:%OS")

#output:
z
"2016-04-10 12:21:25 CEST"

The code is form here: R: convert date from character to datetime

craigster
  • 124
  • 1
  • 6