Problem with date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bluenose
    New Member
    • Apr 2012
    • 56

    Problem with date

    Hello

    I have the following code to display the ordinal date on my pages:

    Code:
    Function ReturnDate() As String
    
            Dim theDate As String = ""
    
            Dim NumericDayofMonth As Integer = Date.Now.Day
            Dim OrdinalSuffix As String = ""
    
            Select Case NumericDayofMonth
                Case 1, 21, 31
                    OrdinalSuffix = "<sup>st</sup>"
                Case 2, 22
                    OrdinalSuffix = "<sup>nd</sup>"
                Case 3, 23
                    OrdinalSuffix = "<sup>rd</sup>"
                Case Else
                    OrdinalSuffix = "<sup>th</sup>"
            End Select
    
            Dim NumericMonthofYear As Integer = Date.Now.Month
            Dim MonthofYear As String = ""
    
            Select Case NumericMonthofYear
                Case 1
                    MonthofYear = "January"
                Case 2
                    MonthofYear = "February"
                Case 3
                    MonthofYear = "March"
                Case 4
                    MonthofYear = "April"
                Case 5
                    MonthofYear = "May"
                Case 6
                    MonthofYear = "June"
                Case 7
                    MonthofYear = "July"
                Case 8
                    MonthofYear = "August"
                Case 9
                    MonthofYear = "September"
                Case 10
                    MonthofYear = "October"
                Case 11
                    MonthofYear = "November"
                Case 12
                    MonthofYear = "December"
            End Select
    
    
            theDate &= DateTime.Now.Day.ToString() '1, 2, 3, 4 etc
            theDate &= OrdinalSuffix & " " 'st, nd, rd, th
            theDate &= MonthofYear & " " 'Jan, Feb etc
            theDate &= DateTime.Now.Year.ToString() '2013, 2014 etc
    
            Return theDate
    
        End Function
    The code works but is showing the time in Los Angeles which is thousands of miles from me (time difference = I am 8 hours ahead even if the servers are not).

    Would I need to add +8 to this line:

    Code:
    theDate &= DateTime.Now.Day.ToString()
    Thanks!
  • IronRazer
    New Member
    • Jan 2013
    • 83

    #2
    Your problem with the day being off could be due to what is said in the 1st paragraph under the Remarks section of the DateTime.Day property in the msdn documents. See the documents at the link below.

    DateTime.Day Property

    However, your whole function could be reduced quite a lot if you just used the ToString method with the custom date and time format strings. See the msdn link below.

    Custom Date and Time Format Strings

    For example...
    Code:
        Private Function GetDate() As String
            Dim suffix As String = "th"
            Select Case Now.Day
                Case 1, 21, 31
                    suffix = "st"
                Case 2, 22
                    suffix = "nd"
                Case 3, 23
                    suffix = "rd"
            End Select
    
            Return Now.ToString("d'" & suffix & "' MMMM yyyy")
        End Function
    This might also fix your problem of the Day being off too.

    Comment

    Working...