data difference in `as.POSIXct` with Excel

后端 未结 4 916
眼角桃花
眼角桃花 2020-12-10 16:32

My actual data looks like:

8/8/2013 15:10
7/26/2013 10:30
7/11/2013 14:20
3/28/2013 16:15
3/18/2013 15:50

When I read this from the excel f

4条回答
  •  被撕碎了的回忆
    2020-12-10 17:21

    This is how it works over here on a Windows system. This is what a source Excel 2010 file looks like:

    date                num         secs        constant    Rtime
    (mm/dd/yyyy)        (in Excel)  (num*86400) (Windows)   (secs-constant) 
    08/08/2013 15:10    41494.63    3585136200  2209161600  1375974600
    07/26/2013 10:30    41481.44    3583996200  2209161600  1374834600
    11/07/2013 14:20    41585.60    3592995600  2209161600  1383834000
    03/28/2013 16:15    41361.68    3573648900  2209161600  1364487300
    03/18/2013 15:50    41351.66    3572783400  2209161600  1363621800
    
    Rtime <- c(1375974600,1374834600,1383834000,1364487300,1363621800)
    as.POSIXct(Rtime,origin="1970-01-01",tz="GMT")
    #[1] "2013-08-08 15:10:00 GMT" "2013-07-26 10:30:00 GMT"
    #[3] "2013-11-07 14:20:00 GMT" "2013-03-28 16:15:00 GMT"
    #[5] "2013-03-18 15:50:00 GMT"
    

    Why this constant? Firstly, because Excel and Office generally is a mess when dealing with dates. Seriously, look over here: Why is 1899-12-30 the zero date in Access / SQL Server instead of 12/31?

    2209161600 is the difference in seconds between the POSIXct start of 1970-01-01 and 1899-12-30, which is the 0 point in Excel on Windows.

    dput(as.POSIXct(2209161600,origin="1899-12-30",tz="GMT"))
    #structure(0, tzone = "GMT", class = c("POSIXct", "POSIXt"))
    

提交回复
热议问题