0

How can I convert a chr value such as: "Thu Jul 24 11:58:12 CEST 2014" to Date format in R?

I've tried:

dataset$acquiredDateTime = strptime(dataset$acquiredDateTime, "%a %b %d %H:%M:%S %Y")

but it returns:

acquiredDateTime : POSIXt, format NA NA NA NA ...

Even as. Date:

dataset$acquiredDateTime = as.Date(dataset$acquiredDateTime, format="%m/%d/%Y %H:%M:%S")

is not working. It returns:

acquiredDateTime : Date, format NA NA NA NA ...
mlibby
  • 6,567
  • 1
  • 32
  • 41
Ricky
  • 285
  • 5
  • 17

3 Answers3

1

You need to include the CEST string in your template, or process it out:

x="Thu Jul 24 11:58:12 CEST 2014"
strptime(x, "%a %b %d %H:%M:%S CEST %Y")

[1] "2014-07-24 11:58:12 UTC"

I don't have experience with interpreting timezones for input.

mdsumner
  • 29,099
  • 6
  • 83
  • 91
0

This worked perfectly fine for me.

strptime("Thu Jul 24 11:58:12 CEST 2014", "%a %b %d %H:%M:%S")
[1] "2014-07-24 11:58:12 IST"

EDIT 1: Ok, I just saw the "CEST" part. Let me update the answer in a bit.

EDIT 2: Other answers have covered it.

Avinash
  • 2,521
  • 4
  • 21
  • 35
0

You could try:

 v1 <- "Thu Jul 24 11:58:12 CEST 2014"

 strptime(gsub("CEST", "", v1), "%a %b %d %H:%M:%S %Y", tz="CEST")
 #[1] "2014-07-24 11:58:12 CEST"
akrun
  • 874,273
  • 37
  • 540
  • 662
  • on my system it works like this: > v1 <- "Thu Jul 24 11:58:12 CEST 2014" > strptime(gsub("CEST", "", v1), "%a %b %d %H:%M:%S %Y", tz="CEST") [1] NA – Ricky Aug 05 '14 at 13:17
  • @Ricky Seems like both (mdsumner) the solutions that are working are not working at your end. Check if this has anything to do with – akrun Aug 05 '14 at 13:19
  • Yes, I fixed it with Sys.setlocale("LC_TIME", "en_GB.UTF-8") thanks. – Ricky Aug 05 '14 at 13:49