R-如何从给定日期获取星期和月份的结束日期?

时间:2019-08-22 08:05:37

标签: r date datetime lubridate

在R中,如何获取星期,两周(也是开始日期),月,季度和年份的结束日期。

比方说,我们的日期为“ 2017年7月15日”,下面的代码获取开始日期。

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.all; --DO NOT FORGET THIS LIBRARY TO USE THE CONVERSION FUNCTION

entity m1 is
    port(
        a :in std_logic;
        b :in std_logic;
        q :out std_logic_vector(7 downto 0)
    );
end entity;

architecture rtl of m1 is
    signal sel :std_logic_vector(1 downto 0);
begin
    sel <= a & b;

    process(sel)
    begin
        case sel is
        when "00" => q <= std_logic_vector(to_unsigned(1,q'length));
        when "01" => q <= std_logic_vector(to_unsigned(2,q'length));
        when "10" => q <= std_logic_vector(to_unsigned(3,q'length));
        when "11" => q <= std_logic_vector(to_unsigned(4,q'length));
        when others => q <= std_logic_vector(to_unsigned(1,q'length));
        end case;
   end process;

end architecture;

在一周的开始日期加上7天可能会给我一周的结束日期。

但是,对于一个月来说,加法根本不是一件好事(因为有些月份有30天,有些31天,有些28天(有时29遍)),而且这种情况只会持续几个季度和几年。

1 个答案:

答案 0 :(得分:0)

ceiling_date包中的

lubridate返回下一个期间的第一个日期。减去1得到当前期间的最后日期。

library(lubridate)

a_date <- as.Date("15-07-17", "%d-%m-%y")

ceiling_date(a_date, "week", week_start = getOption("lubridate.week.start", 1))-1
[1] "2017-07-16"

ceiling_date(a_date, "month")-1
[1] "2017-07-31"

ceiling_date(a_date, "quarter")-1
[1] "2017-09-30"

ceiling_date(a_date, "year")-1
[1] "2017-12-31"