将序数转换为日期 (YY/MM/DD)
Posted
技术标签:
【中文标题】将序数转换为日期 (YY/MM/DD)【英文标题】:Convert ordinals to date (YY/MM/DD) 【发布时间】:2020-12-18 23:10:45 【问题描述】:我将 1988 年 1 月 1 日至 1988 年 12 月 31 日转换为序数,相当于 Matlab 的 datenum 函数:
datenum_vect <- (as.numeric(as.Date(ISOdate(years_vector[1], 1, 1, 0)))+ 719529):
as.numeric(as.Date(ISOdate(tail(years_vector,n=1), 12, 31, 0))+ 719529)
现在,我需要转换回 YEAR-DAY-MONTH (1988-01-01) 形式。我试过这样:
format(as.Date(datenum_vect - 719529, origin = "1988-01-01"), '%b-%Y')
但它不起作用。有什么想法吗?
【问题讨论】:
您能否更具体地说明“它不起作用”。 【参考方案1】:您可以简单地使用as.Date
,但您需要将原点设置为 0 才能执行此操作。此外,某处存在一个错误,因此您需要从 datenum_vec
中减去一天
years_vector <- 1988
datenum_vect <- (as.numeric(as.Date(ISOdate(years_vector[1], 1, 1, 0)))+ 719529):
as.numeric(as.Date(ISOdate(tail(years_vector,n=1), 12, 31, 0))+ 719529)
as.Date(datenum_vect - 1, origin = "0000-01-01")
#> [1] "1988-01-01" "1988-01-02" "1988-01-03" "1988-01-04" "1988-01-05"
#> [6] "1988-01-06" "1988-01-07" "1988-01-08" "1988-01-09" "1988-01-10"
#> [11] "1988-01-11" "1988-01-12" "1988-01-13" "1988-01-14" "1988-01-15"
#> [16] "1988-01-16" "1988-01-17" "1988-01-18" "1988-01-19" "1988-01-20"
#> [21] "1988-01-21" "1988-01-22" "1988-01-23" "1988-01-24" "1988-01-25"
#> [26] "1988-01-26" "1988-01-27" "1988-01-28" "1988-01-29" "1988-01-30"
#> [31] "1988-01-31" "1988-02-01" "1988-02-02" "1988-02-03" "1988-02-04"
#> [36] "1988-02-05" "1988-02-06" "1988-02-07" "1988-02-08" "1988-02-09"
#> [41] "1988-02-10" "1988-02-11" "1988-02-12" "1988-02-13" "1988-02-14"
#> [46] "1988-02-15" "1988-02-16" "1988-02-17" "1988-02-18" "1988-02-19"
#> [51] "1988-02-20" "1988-02-21" "1988-02-22" "1988-02-23" "1988-02-24"
#> [56] "1988-02-25" "1988-02-26" "1988-02-27" "1988-02-28" "1988-02-29"
#> [61] "1988-03-01" "1988-03-02" "1988-03-03" "1988-03-04" "1988-03-05"
#> [66] "1988-03-06" "1988-03-07" "1988-03-08" "1988-03-09" "1988-03-10"
#> [71] "1988-03-11" "1988-03-12" "1988-03-13" "1988-03-14" "1988-03-15"
#> [76] "1988-03-16" "1988-03-17" "1988-03-18" "1988-03-19" "1988-03-20"
#> [81] "1988-03-21" "1988-03-22" "1988-03-23" "1988-03-24" "1988-03-25"
#> [86] "1988-03-26" "1988-03-27" "1988-03-28" "1988-03-29" "1988-03-30"
#> [91] "1988-03-31" "1988-04-01" "1988-04-02" "1988-04-03" "1988-04-04"
#> [96] "1988-04-05" "1988-04-06" "1988-04-07" "1988-04-08" "1988-04-09"
#> [101] "1988-04-10" "1988-04-11" "1988-04-12" "1988-04-13" "1988-04-14"
#> [106] "1988-04-15" "1988-04-16" "1988-04-17" "1988-04-18" "1988-04-19"
#> [111] "1988-04-20" "1988-04-21" "1988-04-22" "1988-04-23" "1988-04-24"
#> [116] "1988-04-25" "1988-04-26" "1988-04-27" "1988-04-28" "1988-04-29"
#> [121] "1988-04-30" "1988-05-01" "1988-05-02" "1988-05-03" "1988-05-04"
#> [126] "1988-05-05" "1988-05-06" "1988-05-07" "1988-05-08" "1988-05-09"
#> [131] "1988-05-10" "1988-05-11" "1988-05-12" "1988-05-13" "1988-05-14"
#> [136] "1988-05-15" "1988-05-16" "1988-05-17" "1988-05-18" "1988-05-19"
#> [141] "1988-05-20" "1988-05-21" "1988-05-22" "1988-05-23" "1988-05-24"
#> [146] "1988-05-25" "1988-05-26" "1988-05-27" "1988-05-28" "1988-05-29"
#> [151] "1988-05-30" "1988-05-31" "1988-06-01" "1988-06-02" "1988-06-03"
#> [156] "1988-06-04" "1988-06-05" "1988-06-06" "1988-06-07" "1988-06-08"
#> [161] "1988-06-09" "1988-06-10" "1988-06-11" "1988-06-12" "1988-06-13"
#> [166] "1988-06-14" "1988-06-15" "1988-06-16" "1988-06-17" "1988-06-18"
#> [171] "1988-06-19" "1988-06-20" "1988-06-21" "1988-06-22" "1988-06-23"
#> [176] "1988-06-24" "1988-06-25" "1988-06-26" "1988-06-27" "1988-06-28"
#> [181] "1988-06-29" "1988-06-30" "1988-07-01" "1988-07-02" "1988-07-03"
#> [186] "1988-07-04" "1988-07-05" "1988-07-06" "1988-07-07" "1988-07-08"
#> [191] "1988-07-09" "1988-07-10" "1988-07-11" "1988-07-12" "1988-07-13"
#> [196] "1988-07-14" "1988-07-15" "1988-07-16" "1988-07-17" "1988-07-18"
#> [201] "1988-07-19" "1988-07-20" "1988-07-21" "1988-07-22" "1988-07-23"
#> [206] "1988-07-24" "1988-07-25" "1988-07-26" "1988-07-27" "1988-07-28"
#> [211] "1988-07-29" "1988-07-30" "1988-07-31" "1988-08-01" "1988-08-02"
#> [216] "1988-08-03" "1988-08-04" "1988-08-05" "1988-08-06" "1988-08-07"
#> [221] "1988-08-08" "1988-08-09" "1988-08-10" "1988-08-11" "1988-08-12"
#> [226] "1988-08-13" "1988-08-14" "1988-08-15" "1988-08-16" "1988-08-17"
#> [231] "1988-08-18" "1988-08-19" "1988-08-20" "1988-08-21" "1988-08-22"
#> [236] "1988-08-23" "1988-08-24" "1988-08-25" "1988-08-26" "1988-08-27"
#> [241] "1988-08-28" "1988-08-29" "1988-08-30" "1988-08-31" "1988-09-01"
#> [246] "1988-09-02" "1988-09-03" "1988-09-04" "1988-09-05" "1988-09-06"
#> [251] "1988-09-07" "1988-09-08" "1988-09-09" "1988-09-10" "1988-09-11"
#> [256] "1988-09-12" "1988-09-13" "1988-09-14" "1988-09-15" "1988-09-16"
#> [261] "1988-09-17" "1988-09-18" "1988-09-19" "1988-09-20" "1988-09-21"
#> [266] "1988-09-22" "1988-09-23" "1988-09-24" "1988-09-25" "1988-09-26"
#> [271] "1988-09-27" "1988-09-28" "1988-09-29" "1988-09-30" "1988-10-01"
#> [276] "1988-10-02" "1988-10-03" "1988-10-04" "1988-10-05" "1988-10-06"
#> [281] "1988-10-07" "1988-10-08" "1988-10-09" "1988-10-10" "1988-10-11"
#> [286] "1988-10-12" "1988-10-13" "1988-10-14" "1988-10-15" "1988-10-16"
#> [291] "1988-10-17" "1988-10-18" "1988-10-19" "1988-10-20" "1988-10-21"
#> [296] "1988-10-22" "1988-10-23" "1988-10-24" "1988-10-25" "1988-10-26"
#> [301] "1988-10-27" "1988-10-28" "1988-10-29" "1988-10-30" "1988-10-31"
#> [306] "1988-11-01" "1988-11-02" "1988-11-03" "1988-11-04" "1988-11-05"
#> [311] "1988-11-06" "1988-11-07" "1988-11-08" "1988-11-09" "1988-11-10"
#> [316] "1988-11-11" "1988-11-12" "1988-11-13" "1988-11-14" "1988-11-15"
#> [321] "1988-11-16" "1988-11-17" "1988-11-18" "1988-11-19" "1988-11-20"
#> [326] "1988-11-21" "1988-11-22" "1988-11-23" "1988-11-24" "1988-11-25"
#> [331] "1988-11-26" "1988-11-27" "1988-11-28" "1988-11-29" "1988-11-30"
#> [336] "1988-12-01" "1988-12-02" "1988-12-03" "1988-12-04" "1988-12-05"
#> [341] "1988-12-06" "1988-12-07" "1988-12-08" "1988-12-09" "1988-12-10"
#> [346] "1988-12-11" "1988-12-12" "1988-12-13" "1988-12-14" "1988-12-15"
#> [351] "1988-12-16" "1988-12-17" "1988-12-18" "1988-12-19" "1988-12-20"
#> [356] "1988-12-21" "1988-12-22" "1988-12-23" "1988-12-24" "1988-12-25"
#> [361] "1988-12-26" "1988-12-27" "1988-12-28" "1988-12-29" "1988-12-30"
#> [366] "1988-12-31"
由reprex package (v0.3.0) 于 2020 年 8 月 30 日创建
【讨论】:
以上是关于将序数转换为日期 (YY/MM/DD)的主要内容,如果未能解决你的问题,请参考以下文章
编写一个程序, 接收用户录入的日期信息并且将其显示出来. 其中, 输入日期的形式为月/日/年(mm/dd/yy), 输出日期的形式为年月日(yy.mm.dd)。