0

I have a character variable,named date with values like "2015-10-17T02:00:00" I want to convert this to date and time. I used the following,

as.POSIXct(format(date, format = "%Y-%m-%d %H:%M:%S"))

But I am getting, "2015-10-17 PDT" I am losing the time factor here. Can anybody help me what mistake I am doing here?

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
haimen
  • 1,985
  • 7
  • 30
  • 53

1 Answers1

1

The format should reflect the exact layout of the data. Your data has "year month day" separated by hyphen, then "T", then "hour minutes and seconds" separated by colon. Your format should show that exactly.

as.POSIXct("2015-10-17T02:00:00", format = "%Y-%m-%dT%H:%M:%S")
#[1] "2015-10-17 02:00:00 EDT"

edit

The format "%Y-%m-%d" can be shortened to "%F", and "%H:%M:%S" can be replaced with "%T".

Pierre L
  • 28,203
  • 6
  • 47
  • 69
  • Now I am able to get the format. I am not able to accept the answer in 10 mins. Would accept it. In the mean time, I tried, typeof(date) -- > Double I am getting as double. Can't I get as Date ? – haimen Oct 19 '15 at 01:44
  • 1
    try `class(date)`. The function `typeof` is pointing to the numbers underneath the data, i.e. "time in seconds from the origin date". – Pierre L Oct 19 '15 at 01:46
  • Thanks... Now i am able to find it. – haimen Oct 19 '15 at 01:48