DJJ
DJJ

Reputation: 2539

R format date with time stamp

I would like to convert the following dates

dates <-c(1149318000L, 1151910000L, 1154588400L, 1157266800L, 1159858800L, 1162540800L)

into date and time format

I don't know the origin of the date but I know that

1146685218 = 2006/05/03 07:00:00

** Update 1 **

I have sorted the unformatted dates and replace the sample above with a friendly sequence but I have real dates. I was thinking of using the the above key as origin, but It does not seem to work.

let

 seconds_in_days  <- 3600*24

(dates[2]-dates[1])/seconds_in_days

## [1] 30

Upvotes: 0

Views: 115

Answers (1)

MrFlick
MrFlick

Reputation: 206167

If you know

1146685218 = 2006/05/03 07:00:00

then just make that the origin

dates <- c(1149318000L, 1151910000L, 1154588400L, 1157266800L, 1159858800L, 1162540800L)

orig.int <- 1146685218
orig.date <- as.POSIXct("2006/05/03 07:00:00", format="%Y/%m/%d %H:%M:%S")

as.POSIXct(dates-orig.int, origin=orig.date)
# [1] "2006-06-02 18:19:42 EDT" "2006-07-02 18:19:42 EDT" "2006-08-02 18:19:42 EDT"
# [4] "2006-09-02 18:19:42 EDT" "2006-10-02 18:19:42 EDT" "2006-11-02 18:19:42 EST"

This works assuming your "date" values are the number of seconds since a particular date/time which is how POSIXt stores it's date/time values.

Upvotes: 1

Related Questions