如何知道今天是本月的周一,周二,周三等(数量)?

时间:2016-06-08 09:54:56

标签: vb.net date datetime dayofweek

我的问题是:

今天的日期和日期是:2016年6月8日星期三。我想知道今天是本月的哪个星期三(如第2个星期三)。我怎么能用VB .NET做到这一点?

我尝试过提供的解决方案:

How do I get first, second or last Tuesday (or any day of the week) of a given month

此解决方案接受"当天的名称"和"每周的一周数"并提供输出作为日期。我希望结果不同,因为我应该输入日期,结果应该是"每日的数字"和"当天名称",像"今天是第二个星期三"。

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

试试这个:

    Sub Main()
        Dim checkDate As DateTime = DateTime.Now

        Dim infos As New Dictionary(Of Integer, String)() From {{1, "st"}, {2, "nd"}, {3, "rd"}, {4, "th"}, {5, "th"}}
        Dim dayCount As Integer = (checkDate.Day \ 7) + Convert.ToInt32((checkDate.Day Mod 7) > 0)
        Console.WriteLine("Today is the {0}{1} {2} of the month", dayCount, infos(dayCount), checkDate.DayOfWeek)

        Console.ReadLine()
    End Sub

答案 1 :(得分:0)

最短路?

    ' 1st: You would need this:
    Dim numbers() As String = {"1st", "2nd", "3rd", "4th", "5th"}
    For day As Integer = 1 To 30
        Dim dat As DateTime = New DateTime(2016, 6, day)

        ' 2nd: The loop was only for test purposes
        Console.WriteLine("Today is {0} {1}", numbers(day \ 7), dat.DayOfWeek.ToString())
    Next day