Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Functions 1

Status
Not open for further replies.

JohannIcon

Programmer
Sep 3, 2002
440
MT
Dear All,

I am trying to format today's date to read as follows:-

Tuesday April 8th 2003.

I managed to create this:-

'*** Get the current year
todayYear = Year(Date())
' get the current month name
todayMonth = (MonthName(month(date)))
' get the current day name
todayDay = (WeekdayName(weekday(date)))
todayDayNum = Day(Date())
todayDateFormat = todayDay & " " & todayMonth & " " & todayDayNum & " " & todayYear
and this works fine.

However I was wondering if there is a way i can get the "th" after the 8 for example. What I want to do is for example if the day is 1, then I attach "st" with it, if it is 2, I attach "nd" with it, if it is 3, I attach "rd" with it etc. Is there a predefined function to do this?

Thanks for your help and time
 
There is no function that will return exactly this format.

FormatDateTime(Date(), 1) will give the result "Tuesday, April 08, 2003". Is this close enough? --James
 
Yeah I managed to get that James, however I wished to get also the st, nd, rd etc.

Thanks just the same but!
 
OK, I wrote this little function to get that format:

Code:
Function LongDate(dt)
  dnm = WeekDayName(WeekDay(dt))
  mnm = MonthName(Month(dt))
  d = Day(dt)
  y = Year(dt)
  
  Select Case Right(d, 1)
    Case 1
      sufx = "st"
    Case 2
      sufx = "nd"
    Case 3
      sufx = "rd"
    Case Else
      sufx = "th"
  End Select

  LongDate = dnm & " " & mnm & " " & d & sufx & " " & y
End Function
--James
 
Just noticed a slight problem with 11, 12, 13 of the month. Changed it slightly:

Code:
Function LongDate(dt)
  dnm = WeekDayName(WeekDay(dt))
  mnm = MonthName(Month(dt))
  d = Day(dt)
  y = Year(dt)
  
  If d <> 11 And d <> 12 And d <> 13 Then
    Select Case Right(d, 1)
      Case 1
        sufx = &quot;st&quot;
      Case 2
        sufx = &quot;nd&quot;
      Case 3
        sufx = &quot;rd&quot;
      Case Else
        sufx = &quot;th&quot;
    End Select
  Else
    sufx = &quot;th&quot;
  End If

  LongDate = dnm & &quot; &quot; & mnm & &quot; &quot; & d & sufx & &quot; &quot; & y
End Function
--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top