46,837 Members
0 added today
536,875 Resources
399 added today

All Devdex   All Gurus  

VBScript Date Formating Class
Author: John Lovell
Rating:
Visits: 24175

Discuss in Newsgroups

<%
'=============================================================
' cvDateFormat
' Author : John Lovell
' Company : CodeValve.com
' Date  : NOV 2002
' NOTES:
' This class was created to have a more controlled date format
'
' You are free to use this code anywhere as long as it stays
' unchanged and this comment block remains.
' This class is posted as is.
' Use it at your own risk.
' There are NO warrantees or guarantees

' associated with it's use.
'
'=============================================================
'
' Format strings : Date = 11/01/2003 (November 1st 2003)
'
' "yyyy/mm/dd"  = 2003/11/01
' "dd/mm/yy"   = 01/11/03
' "dd/mm/yyyy"  = 01/11/2003
' "yyyy-mm-dd"  = 2003-11-03
' "yy-mm-dd"  = 03-11-01
' "dd-mm-yy"  = 01-11-03
' "dd-mm-yyyy"  = 01-11-2003
' "ddmmyyyy"  = 01112003
' "ddmmyy"  = 011103
' "mmddyy"  = 110103
' "mmddyyyy"  = 11012003
' "yyyymmdd"  = 20031101
' "yymmdd"  = 031101
' "yyyy"   = 2003
' "Short"   = 1/11/2003
' "Long"   = Saturday, November 01, 2003
' "SiteDate"  = Saturday, 1st of November, 2003
' "dd-Month-yyyy"  = 01-Nov-2003
' "dd-Month-yy"  = 01-Nov-03
' "DayName"  = Saturday
' "DayNameAbbr"  = Sat
'
'
'===============EXAMPLE====================================
'Set cvDate = new cvDateFormat
'Response.Write(cvDate.fixDate("11/01/2003","SiteDate"))
'Set cvDate = Nothing
'==========================================================


Class cvDateFormat

 ' Use: fixDate(valid date string, format string)

 Public Function fixDate(strDate,format)
  
  d = DatePart("D",strDate)
  m = DatePart("M",strDate)
  y = DatePart("YYYY",strDate)
  
    
  if len(d) < 2 then
   d = "0" & d
  end if
  
  if len(m) < 2 then
   m = "0" & m
  end if
  Select Case Format
   Case "yyyy/mm/dd"
    fixDate = y & "/" & m & "/" & d
   Case "yy/mm/dd"
    fixDate = right(y,2) & "/" & m & "/" & d
   Case "dd/mm/yy"
    fixDate =  d & "/" & m & "/" &  right(y,2)
   Case "dd/mm/yyyy"
    fixDate =  d & "/" & m & "/" &  y
   Case "yyyy-mm-dd"
    fixDate = y & "-" & m & "-" & d
   Case "yy-mm-dd"
    fixDate = right(y,2) & "-" & m & "-" & d
   Case "dd-mm-yy"
    fixDate =  d & "-" & m & "-" &  right(y,2)
   Case "dd-mm-yyyy"
    fixDate =  d & "-" & m & "-" &  y
   Case "ddmmyyyy"
    fixDate =  d & m & y
   Case "ddmmyy"
    fixDate =  d & m & right(y,2)
   Case "mmddyy"
    fixDate =  m & d & right(y,2)
   Case "mmddyyyy"
    fixDate =  m & d & y
   Case "yyyymmdd"
    fixDate = y &  m & d
   Case "yymmdd"
    fixDate = right(y,2) & m & d
   Case "yyyy"
    fixDate = y
   Case "Short"
    fixDate = formatdatetime(strDate,vbShortDate)
   Case "Long"
    fixDate = formatdatetime(strDate,vbLongDate)
   Case "dd-Month-yyyy"
    m = MonthName (m,True)
    fixDate = d & "-" & m & "-" & y
   Case "dd-Month-yy"
    m = MonthName (m,True)
    fixDate = d & "-" & m & "-" & right(y,2)
   Case "DayName"
    fixDate = WeekDayName(Weekday(strDate),False)
   Case "DayNameAbbr"
    fixDate = WeekDayName(Weekday(strDate),True)
   Case "SiteDate"
    fixDate = WeekDayName(Weekday(strDate),False) & ", " & DateSuffix(DatePart("D",strDate)) & " of " & MonthName(m,false) & ", " & fixDate(strDate,"yyyy")
   Case "Stamp"
    fixDate = fixdate(Now(),"yyyymmdd") & fixTime(Now(),"Stamp")
   Case Else
    fixDate =  d & "/" & m & "/" &  y
  End Select
  
 End Function
 
 
 Private Function DateSuffix(num)
 
  Dim x
  
  if num < 13 or num > 20 then
   Select Case right(num,1)
    Case "0"
     x = "th"
    Case "1"
     x = "st"
    Case "2"
     x = "nd"
    Case "3"
     x = "rd"
    Case else
     x = "th"
   End Select
  End if
  
  If num > 12 and num < 21 then
   x = "th"
  End If
  
  DateSuffix = num & x 
  
 
 End Function
 
 Public Function fixTime(strTime,format)
  h = Hour(strTime)
  m = Minute(strTime)
  s = Second(strTime)
  
  
  if s < 10 then
   s = "0" & s
  end if
  
  if m < 10 then
   m = "0" & m
  end if
  
  if h < 10 then
   h = "0" & h
  end if
  
  Select Case format
   Case "hh:mm:ss"
    fixTime = h & ":" & m & ":" & s
   Case "hhmmss"
    fixTime = h & m & s
   Case "Stamp"
    fixTime = h & m & s
   Case Else
    fixTime = formatdatetime(strTime,vbShortTime)
  End Select
  
 End Function

End Class

'Another Example
'Set cvDate = new cvDateFormat
'Response.Write(cvDate.fixDate(Now(),"Stamp"))
'Set cvDate = Nothing


%>


Visit my guru profile

Visitor Comments

Mio U Cool... brilliant... I just add the format "mm-dd...
Steven Burn Quite simply, just fantastic!, cheers John :o) ...
Tom Bruinsma Thanks for the class it has been a great help. It ...
Tom Bruinsma I like the code with one exception. I would add th...

 

Rate this Code Sample






	
	
	



Credit Card Payment Control
Supports over 25 companies
Managed ASP.NET Solution
Direct Processor Support

ASP ArticlesThis category has been added to your weekly newsletter
ASP Web Sites
ADSI & WSH BooksThis category has been added to your weekly newsletter
FREE ComponentsThis category has been added to your weekly newsletter
ASP EventsThis category has been added to your weekly newsletter
ASP HeadlinesThis category has been added to your weekly newsletter

CSharp ArticlesThis category has been added to your weekly newsletter
C# Web SitesThis category has been added to your weekly newsletter

SQL ArticlesThis category has been added to your weekly newsletter
SQL Events
SQL HeadlinesThis category has been added to your weekly newsletter
SQL Jobs

Jobs in CaliforniaThis category has been added to your weekly newsletter

XML ArticlesThis category has been added to your weekly newsletter
XML BooksThis category has been added to your weekly newsletter
XML Web Sites
XML Tutorials

free asp host

"Alex Homer"This search has been added to your weekly newsletter

Edit My Favorites Edit Profile & Favorites

Web Programming

 




Developersdex Home | ASP | C# | SQL | VB | XML | Gurus
Add Your Link | Add Your Code | FAQ | Advertise | Link To Us | Contact Us |
Copyright © 2008 Developersdex™. All rights reserved.