VB and VBA Users Source Code: Formating Dates in ASP to be regionally aware
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Formating Dates in ASP to be regionally aware
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, September 10, 2002
Hits:
808
Category:
Internet/Remote Comms
Article:
Amazing Microsoft made the assumption with ASP that everywhere in the world uses American date formats, i.e. has the month first. The intrinsic "FormatDateTime" function completely ignores regional settings for short dates and always places the month portion first. The following functions provide basic level of date formating: 'Purpose : Returns a formatted date string 'Inputs : dtDate The date to format ' sFormat The format string: ' This can be either slash, space (or colon for time) deliminated string ' eg DateFormat(Now,"dd:mmm:yyyy hh:mm") Returns "11/Sep/2002 11:30" 'Outputs : Returns a distinct value from a range of values 'Author : Andrew Baker 'Date : 04/Sep/2002 'Notes : Amazingly Microsoft provide little useful date format support for ASP. The functionality offered ' ignores regional settings and swaps the months to the start of the date, which is pretty lame ' all things considered. The three functions used by this code attempts to provide a minimum of ' functionality for showing NON US dates (i.e. useful for the rest of the world!) Function DateFormat(dtDate, sFormat) Const csDateSep1 = "/", csDateSep2 = " " Dim asDateParts1, lThisDatePart1 Dim asDateParts2, lThisDatePart2 Dim sDate, sThisDatePart If IsNull(dtDate) = True Or IsDate(dtDate)=False Then DateFormat=" " Else 'Check for csDateSep1 delimated parts asDateParts1 = Split(sFormat, csDateSep1) For lThisDatePart1 = 0 To UBound(asDateParts1) sThisDatePart = DateExtract(dtDate, asDateParts1(lThisDatePart1), csDateSep1) If Len(sThisDatePart) = 0 Then 'Didn't find date part, try using second deliminator 'Check for csDateSep2 delimated parts asDateParts2 = Split(asDateParts1(lThisDatePart1), csDateSep2) For lThisDatePart2 = 0 To UBound(asDateParts2) sThisDatePart = DateExtract(dtDate, asDateParts2(lThisDatePart2), csDateSep2) sDate = sDate & sThisDatePart Next sThisDatePart = "" If lThisDatePart1 <> UBound(asDateParts1) Then 'Replace last separator sDate = Left(sDate, Len(sDate) - 1) & csDateSep1 End If End If sDate = sDate & sThisDatePart Next DateFormat = Left(sDate, Len(sDate) - 1) End If End Function 'Used by DateFormat. 'Returns a section of a date in a specified text format Function DateExtract(dtDate, sDatePart, sSeparator) Dim sDate Select Case LCase(sDatePart) Case "dd" sDate = LeftPad(Day(dtDate),"0",2) & sSeparator Case "ddd" 'eg Wed or Thur sDate = WeekdayName(Weekday(dtDate), 1) & sSeparator Case "dddd" 'eg Wednesday or Thursday sDate = WeekdayName(Weekday(dtDate)) & sSeparator Case "mm" 'Month eg 09 or 11 sDate = LeftPad(Month(dtDate), "0", 2) & sSeparator Case "mmm" 'eg Sep or Nov sDate = MonthName(Month(dtDate), 1) & sSeparator Case "mmmm" 'eg September or November sDate = MonthName(Month(dtDate)) & sSeparator Case "yy" sDate = Right(Year(dtDate), 2) & sSeparator Case "yyy" sDate = Right(Year(dtDate), 3) & sSeparator Case "yyyy" sDate = Year(dtDate) & sSeparator Case "hh" sDate = Hour(dtDate) & sSeparator Case "nn" 'Minute sDate = Minute(dtDate) & sSeparator Case "hh" 'Hour sDate = LeftPad(Hour(dtDate), "0", 2) & sSeparator Case "ss" 'Second sDate = LeftPad(Second(dtDate), "0", 2) & sSeparator Case "hh:nn" sDate = LeftPad(Hour(dtDate), "0", 2) & ":" & LeftPad(Minute(dtDate), "0", 2) & sSeparator Case "hh:nn:ss" sDate = LeftPad(Hour(dtDate), "0", 2) & ":" & LeftPad(Minute(dtDate), "0", 2) & ":" & LeftPad(Second(dtDate), "0", 2) & sSeparator Case "mm:ss" sDate = LeftPad(Minute(dtDate), "0", 2) & LeftPad(Second(dtDate), "0", 2) & sSeparator Case Else sDate = "" End Select DateExtract = sDate End Function 'Used by DateFormat. 'Pads the left portion of a string to make the string a fixed length. Function LeftPad(sString, sChar, lNum) Dim lCharDiff LeftPad = sString lCharDiff = lNum - Len(sString) If lCharDiff > 0 Then LeftPad = String(lCharDiff, sChar) & sString End If End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder