R

时间:2016-12-22 17:57:30

标签: r date

在MySQL中有TO_DAYS函数将日期转换为整数

例如

SELECT TO_DAYS('2016-12-22')

给出736685

R中是否有这个函数的等价物?

2 个答案:

答案 0 :(得分:2)

使用可以采用整数或数字参数的as.Date()。您需要提供适当的原产地。只是客人想象这似乎有效:

R> as.Date(736685, origin="0000-01-01")
[1] "2016-12-22"
R> 

如果要生成与MySQL相同的数字,则需要再次考虑其偏移量:

R> as.numeric(as.Date("2016-12-22"))
[1] 17157
R> as.numeric(as.Date("2016-12-22")) + 
+                  (as.Date("1970-01-01") - as.Date("0000-01-01"))
Time difference of 736685 days
R> as.numeric(as.Date("2016-12-22")) + 
+                  as.numeric((as.Date("1970-01-01") - as.Date("0000-01-01")))
[1] 736685
R> 

答案 1 :(得分:2)

可能你想要

to.days <- function(date.string) difftime(as.Date(date.string), as.Date('0000-01-01'),
                                          units='days')

to.days('2016-12-22')
# Time difference of 736685 days

froms = (PlaceAutocompleteFragment)getFragmentManager().findFragment‌​ById(R.id.froms);