Date conversion in R number to date - r

I have this number 13132800000 and i know that is a birthdate "06/02/1970" or "02 june 1970".
How can i convert this number to this date in R
I have no idea of what kind of date is that
With
Library(zoo)
as.Date(person$birthDate)
[1] "-5877641-06-23"

looks like timestamp in miliseconds
try
as.POSIXct( 13132800000/1000, origin = "1970-01-01", tz = "UTC" )
[1] "1970-06-02 UTC"

Related

How to change a number into datetime format in R

I have a vector a = 40208.64507.
In excel, I can automatically change a to a datetime: 2010/1/30 15:28:54 by click the Date type.
I tried some methods but I cannot get the same result in R, just as in excel.
a = 40208.64507
# in Excel, a can change into: 2010/1/30 15:28:54
as.Date(a, origin = "1899-12-30")
lubridate::as_datetime(a, origin = "1899-12-30")
Is there any way to get the same results in R as in Excel?
Here are several ways. chron class is the closest to Excel in terms of internal representations -- they are the same except for origin -- and the simplest so we list that one first. We also show how to use chron as an intermediate step to get POSIXct.
Base R provides an approach which avoids package dependencies and lubridate might be used if you are already using it.
1) Add the appropriate origin using chron to get a chron datetime or convert that to POSIXct. Like Excel, chron works in days and fractions of a day, but chron uses the UNIX Epoch as origin whereas Excel uses the one shown below.
library(chron)
a <- 40208.64507
# chron date/time
ch <- as.chron("1899-12-30") + a; ch
## [1] (01/30/10 15:28:54)
# POSIXct date/time in local time zone
ct <- as.POSIXct(ch); ct
## [1] "2010-01-30 10:28:54 EST"
# POSIXct date/time in UTC
as.POSIXct(format(ct), tz = "UTC")
## [1] "2010-01-30 10:28:54 UTC"
2) Using only base R convert the number to Date class using the indicated origin and then to POSIXct.
# POSIXct with local time zone
ct <- as.POSIXct(as.Date(a, origin = "1899-12-30")); ct
## [1] "2010-01-30 10:28:54 EST"
# POSIXct with UTC time zone
as.POSIXct(format(ct), tz = "UTC")
## [1] "2010-01-30 15:28:54 UTC"
3) Using lubridate it is similar to base R so we can write
library(lubridate)
# local time zone
as_datetime(as_date(a, origin = "1899-12-30"), tz = "")
[1] "2010-01-30 15:28:54 EST"
# UTC time zone
as_datetime(as_date(a, origin = "1899-12-30"))
[1] "2010-01-30 15:28:54 UTC"

Determine if a date is GMT or BST

I have a bunch of dates (in UTC) e.g.
> dates <- as.POSIXct(c('2019-01-01', '2019-07-01', '2019-08-01'), tz = 'UTC')
> dates
[1] "2019-01-01 UTC" "2019-07-01 UTC" "2019-08-01 UTC"
I want to know if each date in my vector is during GMT or BST.
So the above would give
"GMT" "BST" "BST"
Might not be the best way of doing it, but got what I wanted using
library(lubridate)
ifelse(hour(force_tz(as.POSIXct(as.Date(dates)), tz = 'Europe/London')) == 1, 'BST', 'GMT')

date from number of days in R?

I have col of number of days since 1970-01-01 00:00:00 UTC that I want to convert to date and time UTC.
I tried this:
z <- Sys.time()
j=floor(unclass(z)/86400) # the number of days since 1970-01-01 (UTC)
as.POSIXct(j, origin = "1970-01-01")
[1] "1970-01-01 05:53:22 CET"
But it is not correct. any idea?
You have to pass to the function as.POSIXct the number of seconds, try this code
j<-17148.5625000000
as.POSIXct(j*24*60*60, origin = "1970-01-01", tz="UTC")
[1] "2016-12-13 13:30:00 UTC"
If you want only the day in output
as.Date(as.POSIXct(j*24*60*60, origin = "1970-01-01", tz="UTC"))
[1] "2016-12-13"

Convert DD/MM/YYYY HH:MM into date

I need to transfer a date time read in as a character from a CSV and convert it to a POSIXct format.
I can successfully do with dates only but have been unable to do this for a date and a time combined character string, the time.
time <-('01/08/2014 16:25')
as.POSIXct(time, origin = "03/01/1950 00:00", tz = "GMT")
[1] "0001-08-20 GMT"
class(time2)
[1] "POSIXlt" "POSIXt"
Any pointers would be appreciated!
time <-("01/08/2014 16:25:00")
time2 <- strptime(time,"%d/%m/%Y %H:%M:%S",tz="GMT")
[1] "2014-08-01 16:25:00 GMT"
I was unaware that the %Y has to be capped for 4 digits!

Something wrong in convert ISO 8601 date/times with a 'POSIXct' object

There is something I don't understand. I simply try to convert a date in an other time zone. The date is in the 8601-ISO format. I followed this.
pb.txt <- "2012-09-11T21:23:20Z"
pb.date <- as.POSIXct(pb.txt, tz="UTC")
format(pb.date, tz="WEST",usetz=TRUE)
[1] "2012-09-11 WEST"
Why only the date appears and not anymore the timestamp ?
I tried also :
pb.date <- as.POSIXct(pb.txt, origin=ISOdatetime(2012,09,11,21,23,20))
format(pb.date, tz="WEST",usetz=TRUE)
[1] "2012-09-10 22:00:00 WEST
It's better, but the timestamp is rounded. How to convert perfectly an 8601-ISO datetime ?
Use the correct format:
as.POSIXct(pb.txt, "%Y-%m-%dT%H:%M:%S", tz="UTC")
[1] "2012-09-11 21:23:20 UTC"
In addition to #JoshuaUlrich answer, don't use daylight saving time timezone: use regular time zone, the system will convert automatically if the day chosen falls during summer time.
Given pb.date <- as.POSIXct(pb.txt, "%Y-%m-%dT%H:%M:%S", tz="UTC") as per Joshua's answer, this fails:
format(pb.date, tz="WEST",usetz=TRUE)
[1] "2012-09-11 21:23:20 UTC"
but this doesn't:
format(pb.date, tz="WET", usetz=TRUE)
[1] "2012-09-11 22:23:20 WEST"

Resources