将日期转换为最接近的月末日期 MATLAB

发布于 2024-11-25 17:03:16 字数 404 浏览 2 评论 0原文

我需要将日期数字转换为最接近的月末日期。我找到了一个在线链接,但对于大型矩阵来说效率非常低(位于 http://www.mathworks.com/matlabcentral/fileexchange/26374-round-off-dates-and-times)。 Matlab(Financial Toolbox)是否有内置函数?我找不到它。

date_in = 734421 ;
somefunction(date_in) --> Sept 2010

谢谢!

I need to convert a datenumber to its closest end-of-month date. I found an online link but it is very inefficient for a large matrix (at http://www.mathworks.com/matlabcentral/fileexchange/26374-round-off-dates-and-times). Does Matlab (Financial Toolbox) has an inbuilt function for this? I couldn't find it.

date_in = 734421 ;
somefunction(date_in) --> Sept 2010

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光无声 2024-12-02 17:03:16

基本上,听起来您是在询问给定日期是否接近上个月或下个月。如果使用函数 EOMDAY 来查找,则可以大大简化所涉及的逻辑月底的日期和ADDTODATE 来移动当前月份向上或向下 一。以下是一个以日期数字作为输入的示例函数:

function closestString = closest_month(dateNumber)

  dateVector = datevec(dateNumber);
  daysInMonth = eomday(dateVector(1),dateVector(2));
  if dateVector(3) > daysInMonth/2
    dateNumber = addtodate(dateNumber,1,'month');
  else
    dateNumber = addtodate(dateNumber,-1,'month');
  end
  closestString = datestr(dateNumber,'mmm yyyy');

end

Basically, it sounds like you are asking for whether a given date is closer to the preceding or following month. You can greatly simplify the logic involved if you use the functions EOMDAY to find the date for the end of the month and ADDTODATE to shift the current month up or down by one. Here's an example function that takes a date number as input:

function closestString = closest_month(dateNumber)

  dateVector = datevec(dateNumber);
  daysInMonth = eomday(dateVector(1),dateVector(2));
  if dateVector(3) > daysInMonth/2
    dateNumber = addtodate(dateNumber,1,'month');
  else
    dateNumber = addtodate(dateNumber,-1,'month');
  end
  closestString = datestr(dateNumber,'mmm yyyy');

end
原谅我要高飞 2024-12-02 17:03:16

我之前的版本有一些错误。这是合并到函数中的逻辑。它还检查月份并相应更新。

function out = roundMonth(dateNumber)
    dateVector = datevec(dateNumber);
    day = dateVector(3);
    month = dateVector(2);
    year = dateVector(1);

    month = month + sign(day - 15 + double(~(month-2)))...
        + double(~(day-15 + double(~(month-2))));

    dateVector(1) = year + double((month-12)==1) - double((1-month)==1);
    dateVector(2) = mod(month,12) + 12*double(~mod(month,12));

    out = datestr(dateVector,'mmm yyyy');

示例

1.2.3.4

roundMonth(datenum('10-Oct-2010'))

ans =

Sep 2010

roundMonth(datenum('20-Oct-2010'))

ans =

Nov 2010

.

roundMonth(datenum('20-Dec-2010'))

ans =

Jan 2011

roundMonth(datenum('10-Jan-2010'))

ans =

Dec 2009

I had some errors in my previous version. Here's the logic incorporated into a function. It also checks for the month and updates accordingly.

function out = roundMonth(dateNumber)
    dateVector = datevec(dateNumber);
    day = dateVector(3);
    month = dateVector(2);
    year = dateVector(1);

    month = month + sign(day - 15 + double(~(month-2)))...
        + double(~(day-15 + double(~(month-2))));

    dateVector(1) = year + double((month-12)==1) - double((1-month)==1);
    dateVector(2) = mod(month,12) + 12*double(~mod(month,12));

    out = datestr(dateVector,'mmm yyyy');

EXAMPLES:

1.

roundMonth(datenum('10-Oct-2010'))

ans =

Sep 2010

2.

roundMonth(datenum('20-Oct-2010'))

ans =

Nov 2010

3.

roundMonth(datenum('20-Dec-2010'))

ans =

Jan 2011

4.

roundMonth(datenum('10-Jan-2010'))

ans =

Dec 2009
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文