Remove timezone during POSIXlt Conversion in R

Viewed 14998

I have a column in my dataframe as datetime (factor) with the values as "15-10-2017 16:41:00".

I wanted this data to be converted as "2017-10-15 16:41:00". When i try to convert this, I'm getting the timezone also as output.

I tried using tz="", usetz=F but no use. Any suggestions ?

Code:

as.POSIXlt("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S") [1] "2017-10-15 16:41:00 IST"

3 Answers

If you are ok storing the datetime as a character instead of as a POSIXlt, then you can use strftime():

my_datetime <- as.POSIXlt("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
strftime(my_datetime)

I do it like this:

strip.tz <- function(dt) {
  fmt <- "%Y-%m-%d %H:%M:%S"
  strptime(strftime(dt, format = fmt, tz=""), format = fmt, tz="UTC")
}

and you would use it like this:

my_datetime <- as.POSIXct("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
strip.tz(my_datetime)
Related