如何使用 VB.Net 2.0 在 ASP.Net 中处理季度(季度日期)?

发布于 2024-07-04 18:29:23 字数 1450 浏览 6 评论 0 原文

我知道 Sql Server 有一些方便的内置季度内容,但是什么关于 .Net 本机 DateTime 对象? 加法、减法和遍历四分之一的最佳方法是什么?

使用 VB 特定的 坏事™吗? ="nofollow noreferrer">DateAdd() 函数? 例如:

Dim nextQuarter As DateTime = DateAdd(DateInterval.Quarter, 1, DateTime.Now)

编辑: 扩展 @bslorence 的函数:

Public Shared Function AddQuarters(ByVal originalDate As DateTime, ByVal quarters As Integer) As Datetime
    Return originalDate.AddMonths(quarters * 3)
End Function

扩展 @Matt 的函数:

Public Shared Function GetQuarter(ByVal fromDate As DateTime) As Integer
    Return ((fromDate.Month - 1) \ 3) + 1
End Function

编辑:这里还有几个方便的函数:

Public Shared Function GetFirstDayOfQuarter(ByVal originalDate As DateTime) As DateTime
    Return AddQuarters(New DateTime(originalDate.Year, 1, 1), GetQuarter(originalDate) - 1)
End Function

Public Shared Function GetLastDayOfQuarter(ByVal originalDate As DateTime) As DateTime
    Return AddQuarters(New DateTime(originalDate.Year, 1, 1), GetQuarter(originalDate)).AddDays(-1)
End Function

I know that Sql Server has some handy built-in quarterly stuff, but what about the .Net native DateTime object? What is the best way to add, subtract, and traverse quarters?

Is it a bad thing™ to use the VB-specific DateAdd() function? e.g.:

Dim nextQuarter As DateTime = DateAdd(DateInterval.Quarter, 1, DateTime.Now)

Edit:
Expanding @bslorence's function:

Public Shared Function AddQuarters(ByVal originalDate As DateTime, ByVal quarters As Integer) As Datetime
    Return originalDate.AddMonths(quarters * 3)
End Function

Expanding @Matt's function:

Public Shared Function GetQuarter(ByVal fromDate As DateTime) As Integer
    Return ((fromDate.Month - 1) \ 3) + 1
End Function

Edit: here's a couple more functions that were handy:

Public Shared Function GetFirstDayOfQuarter(ByVal originalDate As DateTime) As DateTime
    Return AddQuarters(New DateTime(originalDate.Year, 1, 1), GetQuarter(originalDate) - 1)
End Function

Public Shared Function GetLastDayOfQuarter(ByVal originalDate As DateTime) As DateTime
    Return AddQuarters(New DateTime(originalDate.Year, 1, 1), GetQuarter(originalDate)).AddDays(-1)
End Function

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

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

发布评论

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

评论(5

野稚 2024-07-11 18:29:23

这个怎么样:

Dim nextQuarter As DateTime = DateTime.Now.AddMonths(3);

How about this:

Dim nextQuarter As DateTime = DateTime.Now.AddMonths(3);
深者入戏 2024-07-11 18:29:23

需要记住的一件事是,并非所有公司都在每月的最后一天结束季度。

One thing to remeber, not all companies end their quarters on the last day of a month.

望她远 2024-07-11 18:29:23
Public Function GetLastQuarterStart() As Date

    GetLastQuarterStart = DateAdd(DateInterval.Quarter, -1, DateTime.Now).ToString("MM/01/yyyy")

End Function

Public Function GetLastQuarterEnd() As Date

    Dim LastQuarterStart As Date = DateAdd(DateInterval.Quarter, -1, DateTime.Now).ToString("MM/01/yyyy")
    Dim MM As String = LastQuarterStart.Month
    Dim DD As Integer = 0
    Dim YYYY As String = LastQuarterStart.Year
    Select Case MM
        Case "01", "03", "05", "07", "08", "10", "12"
            DD = 31
        Case "02"
            Select Case YYYY
                Case "2012", "2016", "2020", "2024", "2028", "2032"
                    DD = 29
                Case Else
                    DD = 28
            End Select
        Case Else
            DD = 30
    End Select

    Dim LastQuarterEnd As Date = DateAdd(DateInterval.Month, 2, LastQuarterStart)

    MM = LastQuarterEnd.Month
    YYYY = LastQuarterEnd.Year

    Return String.Format("{0}/{1}/{2}", MM, DD, YYYY)

End Function
Public Function GetLastQuarterStart() As Date

    GetLastQuarterStart = DateAdd(DateInterval.Quarter, -1, DateTime.Now).ToString("MM/01/yyyy")

End Function

Public Function GetLastQuarterEnd() As Date

    Dim LastQuarterStart As Date = DateAdd(DateInterval.Quarter, -1, DateTime.Now).ToString("MM/01/yyyy")
    Dim MM As String = LastQuarterStart.Month
    Dim DD As Integer = 0
    Dim YYYY As String = LastQuarterStart.Year
    Select Case MM
        Case "01", "03", "05", "07", "08", "10", "12"
            DD = 31
        Case "02"
            Select Case YYYY
                Case "2012", "2016", "2020", "2024", "2028", "2032"
                    DD = 29
                Case Else
                    DD = 28
            End Select
        Case Else
            DD = 30
    End Select

    Dim LastQuarterEnd As Date = DateAdd(DateInterval.Month, 2, LastQuarterStart)

    MM = LastQuarterEnd.Month
    YYYY = LastQuarterEnd.Year

    Return String.Format("{0}/{1}/{2}", MM, DD, YYYY)

End Function
隔纱相望 2024-07-11 18:29:23

扩展马特布莱恩的答案:

Dim intQuarter As Integer = Math.Ceiling(MyDate.Month / 3)

不确定这是否会增加速度优势,但在我看来它看起来更干净

Expanding on Matt Blaine's Answer:

Dim intQuarter As Integer = Math.Ceiling(MyDate.Month / 3)

Not sure if this would add speed benefits or not but it looks cleaner IMO

是你 2024-07-11 18:29:23

我知道您可以通过:

Dim quarter As Integer = (someDate.Month - 1) \ 3 + 1

如果您使用Visual Studio 2008,可以尝试通过查看扩展方法

I know you can calculate the quarter of a date by:

Dim quarter As Integer = (someDate.Month - 1) \ 3 + 1

If you're using Visual Studio 2008, you could try bolting additional functionality on to the DateTime class by taking a look at Extension Methods.

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