如何确定两个日期之间相隔了多少个月、周和天?

发布于 2024-09-06 08:11:44 字数 292 浏览 5 评论 0原文

INFORMIX-SQL 7.32 (SE) 执行屏幕:

假设我的开始日期为 2010 年 2 月 15 日,结束日期为 2010 年 5 月 27 日。我可以使用“let elapsed_days = end_date - start_date”计算经过的天数,但如何将这些天数转换为 3 个月、1 周和 5 天?

我见过使用的原始计算方法,每个月四舍五入为 31 天,因为如果您将每个月的天数相加并除以 12,则平均天数为 30.5 天 每月,然后将经过的天数除以 31 得到 3.31 个月,但这种方法对于我的需求来说是不可接受的。

INFORMIX-SQL 7.32 (SE) Perform screen:

Let's say I have a start date of FEB-15-2010 and an end date of MAY-27-2010. I can calculate the number of elapsed days with 'let elapsed_days = end_date - start_date', but how can I convert these number of days into 3 months, 1 week and 5 days?

A raw calculation I've seen used, rounding every month to 31 days, since if you take the number of days in each month, add them up and divide them by 12 gives you 30.5 days average
per month, then taking elapsed days and dividing it by 31 produces 3.31 months, but this method is unacceptable for my needs.

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

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

发布评论

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

评论(1

浮世清欢 2024-09-13 08:11:44

这可能可以经受一些更严格的测试,并且肯定有空间整理输出(即删除“0 个月”子字符串),但我认为它可以让您大部分顺利完成......

CREATE PROCEDURE informix.datediff(d1 DATE, d2 DATE) RETURNING VARCHAR(255);
    DEFINE yrcount, mthcount, wkcount, daycount INTEGER;
    DEFINE dx DATE;

    LET mthcount = ((YEAR(d2) - YEAR(d1)) * 12) + MONTH(d2) - MONTH(d1);
    IF DAY(d1) <= DAY(d2) THEN
        LET daycount = DAY(d2) - DAY(d1);
    ELSE
        LET dx = MDY(MONTH(d1),1,YEAR(d1))+1 UNITS MONTH;
        LET daycount = dx - d1;     -- elapsed days from last month
        LET daycount = daycount + DAY(d2) - 1; -- elapsed days from this month
    END IF;

    LET yrcount = mthcount / 12;
    LET mthcount = MOD(mthcount,12);
    LET wkcount = daycount / 7;
    LET daycount = MOD(daycount,7);

    RETURN d1 || " - " || d2 || ": " || yrcount || " years, " || mthcount
         || " months, " || wkcount || " weeks and " || daycount || " days ";
END PROCEDURE;

即:

execute procedure datediff(TODAY, "19/03/2011");
(expression)  21/06/2010 - 19/03/2011: 0 years, 9 months, 4 weeks and 0 days

execute procedure datediff(TODAY, "22/03/2011");
(expression)  21/06/2010 - 22/03/2011: 0 years, 9 months, 0 weeks and 1 days

execute procedure datediff("08/02/2010", "08/05/2011");
(expression)  08/02/2010 - 08/05/2011: 1 years, 3 months, 0 weeks and 0 days

execute procedure datediff("31/03/2010", TODAY);
(expression)  31/03/2010 - 21/06/2010: 0 years, 3 months, 3 weeks and 0 days

execute procedure datediff(TODAY-3, TODAY);
(expression)  18/06/2010 - 21/06/2010: 0 years, 0 months, 0 weeks and 3 days

execute procedure datediff(TODAY-33, TODAY);
(expression)  19/05/2010 - 21/06/2010: 0 years, 1 months, 0 weeks and 2 days

This could probably stand some more rigorous testing, and there is certainly scope to tidy up the output (ie remove "0 months" substrings), but I think it gets you most of the way there...

CREATE PROCEDURE informix.datediff(d1 DATE, d2 DATE) RETURNING VARCHAR(255);
    DEFINE yrcount, mthcount, wkcount, daycount INTEGER;
    DEFINE dx DATE;

    LET mthcount = ((YEAR(d2) - YEAR(d1)) * 12) + MONTH(d2) - MONTH(d1);
    IF DAY(d1) <= DAY(d2) THEN
        LET daycount = DAY(d2) - DAY(d1);
    ELSE
        LET dx = MDY(MONTH(d1),1,YEAR(d1))+1 UNITS MONTH;
        LET daycount = dx - d1;     -- elapsed days from last month
        LET daycount = daycount + DAY(d2) - 1; -- elapsed days from this month
    END IF;

    LET yrcount = mthcount / 12;
    LET mthcount = MOD(mthcount,12);
    LET wkcount = daycount / 7;
    LET daycount = MOD(daycount,7);

    RETURN d1 || " - " || d2 || ": " || yrcount || " years, " || mthcount
         || " months, " || wkcount || " weeks and " || daycount || " days ";
END PROCEDURE;

ie:

execute procedure datediff(TODAY, "19/03/2011");
(expression)  21/06/2010 - 19/03/2011: 0 years, 9 months, 4 weeks and 0 days

execute procedure datediff(TODAY, "22/03/2011");
(expression)  21/06/2010 - 22/03/2011: 0 years, 9 months, 0 weeks and 1 days

execute procedure datediff("08/02/2010", "08/05/2011");
(expression)  08/02/2010 - 08/05/2011: 1 years, 3 months, 0 weeks and 0 days

execute procedure datediff("31/03/2010", TODAY);
(expression)  31/03/2010 - 21/06/2010: 0 years, 3 months, 3 weeks and 0 days

execute procedure datediff(TODAY-3, TODAY);
(expression)  18/06/2010 - 21/06/2010: 0 years, 0 months, 0 weeks and 3 days

execute procedure datediff(TODAY-33, TODAY);
(expression)  19/05/2010 - 21/06/2010: 0 years, 1 months, 0 weeks and 2 days
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文