在特定时间创建HTML

时间:2012-12-04 10:26:25

标签: vb.net datetime time

如果有以下代码:

Public Shared Function GetNextWeekDay() As Date
    Dim value As Date = Date.Now
    Do
        value = value.AddDays(1)
    Loop While (value.DayOfWeek = DayOfWeek.Saturday) Or (value.DayOfWeek = DayOfWeek.Sunday)
    Return value
End Function


Public Shared Function DPLoadData() As String
    Dim s As StringBuilder = New StringBuilder("<head><meta http-equiv=""content-type"" content=""text/html;charset=utf-8"" /><META HTTP-EQUIV=""REFRESH"" CONTENT=""900"">")
    s.Append("<style type=""text/css"" media=""all""> body{font-family: Arial;}h4{font-size: 10pt;font-weight: bold;white-space: nowrap;margin-top: 0; margin-bottom: 10px;}")
    s.Append("th{font-size: 9pt;font-weight: normal;text-align: center;white-space: nowrap;}td{font-size: 9pt;}.content td{border: solid 1px #dadada;}")
    s.Append(".content th {border: solid 1px #dadada;background-image: url(""tbl_header_row_bg.gif""); background-repeat: repeat-x; white-space: nowrap;}</style></head>")


  s.Append("<h3>" & "Daily Plan" & "</h3>")
        Dim strCurrDay As String = ""
        s.Append("<h5>" & strCurrDay & "</h5>")



    Dim CurrDateFirstDay As Date = GetNextWeekDay()
    strCurrDay = FormatDateTime(CurrDateFirstDay, DateFormat.LongDate)
    s.Append("<h5>" & strCurrDay & "</h5>")
    s.Append(LoadDataGroupByDate(CurrDateFirstDay))

    Return s.ToString()

End Function

DPLoadData函数生成一个带有表格的HTML文件,并用预订填充它。目前,HTML文件显示明天的预订(例如,如果今天是星期一,则显示星期二的预订,如果今天是星期五,则显示星期一的预订)。

我需要的是HMTL文件是在下午5点生成的。例如:如果今天是星期一,则应在星期一下午5点生成HTML文件,并应显示星期二至星期二下午5点和星期二下午5点的预订,该文件应在周三生成,并应显示星期三的预订,直到星期三下午5点,等等。

我该怎么做?请帮忙。

我的解决方案:

 Public Shared Function GetNextWeekDay() As Date
    Dim value As Date = Date.Now
    Dim intHour As Integer
    Dim intMinute As Integer
    Dim intSecond As Integer


    intHour = 17
    intMinute = 0
    intSecond = 0

    Dim newdatetime As DateTime = New Date(value.Year, value.Month, value.Day, intHour, intMinute, intSecond)

    If DateTime.Now < newdatetime Then

        If value.DayOfWeek = DayOfWeek.Saturday Then
            value = value.AddDays(2)
            Return value
        End If

        If value.DayOfWeek = DayOfWeek.Sunday Then
            value = value.AddDays(1)
            Return value
        End If

        Return value

    ElseIf DateTime.Now > newdatetime Then
        Do
            value = value.AddDays(1)
        Loop While (value.DayOfWeek = DayOfWeek.Saturday) Or (value.DayOfWeek = DayOfWeek.Sunday)
        Return value
    End If
End Function

1 个答案:

答案 0 :(得分:1)

据我了解你的问题,你基本上是在寻找一种在给定时间执行程序的方法吗?如果是这样,请查看cronWindows scheduler,具体取决于您运行此操作系统的操作系统。


更新:所以基本上你只需要比较当前时间并检查它是否在下午5点之前。那么你应该有一个函数,它将数据从当天下午5点返回到明天下午5点。

您可能需要查看this VB tutorial for DateTime。基本上你需要将当前时间与当前日期在下午5点的日期时间进行比较。


更新:只需延长你的if条件,也可以检查今天是星期六还是星期日。这是我刚刚掀起的一小段代码片段。我对VB并不熟悉,所以这可能不是100%正确,但我认为你明白了。

Public Function GetNextWeekDay() As Date
    Dim value As Date = Date.Now
    Dim intHour As Integer
    Dim intMinute As Integer
    Dim intSecond As Integer

    intHour = 17
    intMinute = 0
    intSecond = 0

    Dim newdatetime As DateTime = New Date(value.Year, value.Month, value.Day, intHour, intMinute, intSecond)

    If value.DayOfWeek <> DayOfWeek.Saturday And value.DayOfWeek <> DayOfWeek.Sunday And DateTime.Now < newdatetime Then
        Return value.Date
    Else
        Do
            value = value.AddDays(1)
        Loop While (value.DayOfWeek = DayOfWeek.Saturday) Or (value.DayOfWeek = DayOfWeek.Sunday)
        Return value.Date
    End If
End Function