Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove timezone during POSIXlt Conversion in R

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"

like image 289
Simbu Avatar asked Jul 07 '17 11:07

Simbu


People also ask

What is the difference between POSIXct and POSIXlt?

The POSIXct class stores date/time values as the number of seconds since January 1, 1970, while the POSIXlt class stores them as a list with elements for second, minute, hour, day, month, and year, among others.

What is the purpose of the POSIXct time format?

POSIXct stores date and time in seconds with the number of seconds beginning at 1 January 1970. Negative numbers are used to store dates prior to 1970. Thus, the POSIXct format stores each date and time a single value in units of seconds. Storing the data this way, optimizes use in data.

How do I convert DateTime to date in R?

Method 1: Using as.POSIXct() method The dates are converted to the standard time zone, UTC. A string type date object can be converted to POSIXct object, using them as. POSIXct(date) method in R. “ct” in POSIXct denotes calendar time, it stores the number of seconds since the origin.


2 Answers

From the help page of as.POSIXlt:

"" is the current time zone

which is the default.

That's why it does not work. You could remove the timezone information this way, and it will not show while printing:

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

but I don't understand why you would want to do that. You should convert to GMT if you don't want to worry about the timezone. Also lubridate package has a nice force_tz function if you have to force some specific timezones.

like image 100
Probel Avatar answered Sep 22 '22 06:09

Probel


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)
like image 25
r_alanb Avatar answered Sep 24 '22 06:09

r_alanb