我如何发现给定日期的四分之一 [英] How do I discover the quarter of a given date

查看:88
本文介绍了我如何发现给定日期的四分之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个财政年度的季度

The following are the Quarters for a financial year

April to June          - Q1
July to Sep            - Q2
Oct to Dec             - Q3
Jan to March           - Q4

如果月份输入日期的位置如上所述,我需要按季度号进行输出。

If the month of an input date lies as above I need the output for in terms of Quarter number.

例如,

如果输入日期(例如 1月2日),则需要输出为 Q4

If I give an input date (say january 2nd) , I need the output as Q4.

如果我以( Jun 5 )作为输入,输出应为 Q1

If I give input as (Jun 5), output should give Q1.

根据输入的日期,我需要四分之一数字。

Based on input date I need the Quarter number.

推荐答案

您可以简单地将扩展方法编写为DateTime

You can simply write an extension method to DateTime

public static int GetQuarter(this DateTime date)
{
    if (date.Month >= 4 && date.Month <= 6)
        return 1;
    else if (date.Month >= 7 && date.Month <= 9)
        return 2;
    else if (date.Month >= 10 && date.Month <= 12)
        return 3;
    else 
        return 4;
}

并将其用作

DateTime dt = DateTime.Now;
dt.GetQuarter();

这篇关于我如何发现给定日期的四分之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆