0

I have the data with value of date and time like this,

12-Jul-2004  23:00  12-Jul-2004  23:05  13-Jul-2004  5:30   13-Jul-2004  5:30
13-Jul-2004  23:00  13-Jul-2004  23:30  14-Jul-2004  5:30   14-Jul-2004  5:30
14-Jul-2004  22:30  14-Jul-2004  22:40  15-Jul-2004  5:30   15-Jul-2004  5:30
16-Jul-2004  0:00   16-Jul-2004  0:10   16-Jul-2004  5:20   16-Jul-2004  5:30
17-Jul-2004  1:00   17-Jul-2004  1:15   17-Jul-2004  9:00   17-Jul-2004  9:15

I want to change the Date and time value to time only. I did search for solutions but all failed. I hope someone please give your guide here. Many thanks.

Additional info, the date is not in the same format, like 1-Jul-2004, 11-Jul-2004.

  • A regex plus [this](http://stackoverflow.com/q/22659947/324364) probably. – joran Jun 09 '14 at 14:30
  • more info would be helpful. Is this data just strings or a date class? maybe the example `format(Sys.time(), format = '%H:%M')` would give you some guidance, where the gist is `format(formatIN, formatOUT)` – rawr Jun 09 '14 at 14:32
  • Note that base R does not have a special class for "time only", only "date+time". If you want to leave it as a string/character, that's not a problem, but if you need to use it as a more numeric value, you will want to look into packages that offer time-only classes. – MrFlick Jun 09 '14 at 15:01

1 Answers1

0

You can explicitly convert the dates to POSIX timestamps:

x <- c("12-Jul-2004  23:00", "4-Jul-2004  11:00")
x_formatted <- as.POSIXct(x, format =  "%d-%b-%Y  %H:%M")

And then you can output the POSIX timestamp in a format that only includes the hour and minute:

format(x_formatted, format = "%H:%M")

Result:

"23:00" "11:00"
canary_in_the_data_mine
  • 2,193
  • 2
  • 24
  • 28