By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,763 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

Problem with date

P: 56
Hello

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

Expand|Select|Wrap|Line Numbers
  1. Function ReturnDate() As String
  2.  
  3.         Dim theDate As String = ""
  4.  
  5.         Dim NumericDayofMonth As Integer = Date.Now.Day
  6.         Dim OrdinalSuffix As String = ""
  7.  
  8.         Select Case NumericDayofMonth
  9.             Case 1, 21, 31
  10.                 OrdinalSuffix = "<sup>st</sup>"
  11.             Case 2, 22
  12.                 OrdinalSuffix = "<sup>nd</sup>"
  13.             Case 3, 23
  14.                 OrdinalSuffix = "<sup>rd</sup>"
  15.             Case Else
  16.                 OrdinalSuffix = "<sup>th</sup>"
  17.         End Select
  18.  
  19.         Dim NumericMonthofYear As Integer = Date.Now.Month
  20.         Dim MonthofYear As String = ""
  21.  
  22.         Select Case NumericMonthofYear
  23.             Case 1
  24.                 MonthofYear = "January"
  25.             Case 2
  26.                 MonthofYear = "February"
  27.             Case 3
  28.                 MonthofYear = "March"
  29.             Case 4
  30.                 MonthofYear = "April"
  31.             Case 5
  32.                 MonthofYear = "May"
  33.             Case 6
  34.                 MonthofYear = "June"
  35.             Case 7
  36.                 MonthofYear = "July"
  37.             Case 8
  38.                 MonthofYear = "August"
  39.             Case 9
  40.                 MonthofYear = "September"
  41.             Case 10
  42.                 MonthofYear = "October"
  43.             Case 11
  44.                 MonthofYear = "November"
  45.             Case 12
  46.                 MonthofYear = "December"
  47.         End Select
  48.  
  49.  
  50.         theDate &= DateTime.Now.Day.ToString() '1, 2, 3, 4 etc
  51.         theDate &= OrdinalSuffix & " " 'st, nd, rd, th
  52.         theDate &= MonthofYear & " " 'Jan, Feb etc
  53.         theDate &= DateTime.Now.Year.ToString() '2013, 2014 etc
  54.  
  55.         Return theDate
  56.  
  57.     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:

Expand|Select|Wrap|Line Numbers
  1. theDate &= DateTime.Now.Day.ToString() 
Thanks!
Mar 26 '17 #1
Share this Question
Share on Google+
1 Reply


IronRazer
P: 82
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...
Expand|Select|Wrap|Line Numbers
  1.     Private Function GetDate() As String
  2.         Dim suffix As String = "th"
  3.         Select Case Now.Day
  4.             Case 1, 21, 31
  5.                 suffix = "st"
  6.             Case 2, 22
  7.                 suffix = "nd"
  8.             Case 3, 23
  9.                 suffix = "rd"
  10.         End Select
  11.  
  12.         Return Now.ToString("d'" & suffix & "' MMMM yyyy")
  13.     End Function
  14.  
This might also fix your problem of the Day being off too.
Mar 27 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.