Home > OS >  convert Date to POSIXct from origin
convert Date to POSIXct from origin

Time:10-31

I want to convert date to POSIXct format. In the code below I am converting 1601510400 to date format. I want opposite of it. I want to input 2020-10-01 and Output should be 1601510400

as.Date(as.POSIXct(1601510400, origin = "1970-01-01"))

CodePudding user response:

We could use difftime to get the interval between a and origin.

a <- as.Date(as.POSIXct(1601510400, origin = "1970-01-01"))

as.numeric(difftime(time1 = as.POSIXct(a, tz = "UTC"),
                    time2 = as.POSIXct("1970-01-01", tz = "UTC"),
                    units = "secs"))
[1] 1601510400
  •  Tags:  
  • r
  • Related