GregorianCalendar去年的一周

时间:2015-02-12 17:28:03

标签: c# vb.net excel

我有一张excel表,其中包含2003年的列日期(格式:2015/02/10)和列销售。我想提取(去年,当前一周,今天和今天的名称)销售数字。就像今天是2月12日星期四,相当于"星期四"从去年2月13日开始。   我在下面尝试了我的代码,但我收到错误"无法转换字符串"星期四"加倍"任何想法如何?谢谢

 Dim MyCalendar As Calendar = New GregorianCalendar
    Dim culture = New System.Globalization.CultureInfo("EN-US")
    dim eventcal as new datatable
    Dim dayname As String = culture.DateTimeFormat.GetDayName(Now.DayOfWeek)

    Dim lastCurrentweek = MyCalendar.GetWeekOfYear(
      Now,
      CalendarWeekRule.FirstFourDayWeek,
      DayOfWeek.Sunday)
    Dim lastCurrentyear = MyCalendar.GetYear(Now) - 1
        dim dayrecord = (From Row In eventcal.AsEnumerable
           Where MyCalendar.GetWeekOfYear(Row.Field(Of DateTime)("date"),
           CalendarWeekRule.FirstFourDayWeek,
            DayOfWeek.Sunday) = Currentweek And MyCalendar.GetYear(Row.Field(Of DateTime)("date")) = lastCurrentyear And MyCalendar.GetDayOfWeek(Row.Field(Of DateTime)("date")) = dayname
           Select Row.Field(Of Double)("sales"))

1 个答案:

答案 0 :(得分:0)

对于它可能感兴趣的人,这里是你如何得到相当于去年或前一年的今天。感谢tommy两次为这个解决方案提供培训

Private Function Date2Weekday(ByVal d As Date, ByVal y As Integer) As Date
    'determine week
    Dim weekNumber As Integer = Math.Ceiling(d.Day / 7)
    'determine day number
    Dim daynumber1 As Integer = Weekday(d)
    'determine target first day of month
    Dim targetDay1 As Date = New DateTime(y, d.Month, 1)
    Dim targetDay1Number As Integer = Weekday(targetDay1)
    Dim dt As Integer = (weekNumber * 7) + (daynumber1 - targetDay1Number)+1
    If dt > weekNumber * 7 Then dt -= 7
 Date2Weekday = New DateTime(y, d.Month, dt)     
 End Function

Private Function salescalendar(ByVal dtduty As DataTable) As DataTable
   Dim eventcal As New DataTable
    Dim lastyear = MyCalendar.GetYear(Now) - 1
    Dim todays As Date = Now.Date
 Dim dayrecord = (From Row In eventcal.AsEnumerable
                 Where Row.Field(Of DateTime)("date") = Date2Weekday(todays, lastyear)
                Select Row.Field(Of Double)("sales")) 
  end function