• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

format date

Hi.. Can anybody tell me how can I format my datetime object like eg  1-Jan-2013
1 Solution
If this is classic asp, it has a function called formatdatetime
Response.Write FormatDateTime(Now(), 0)

0 could be 0-4, depending on the format you want.
kpankaj9Author Commented:
Hey Gawai
Thanks for response. I m using ASP.Net 1.1/C# and there I could not use ur code.Can you pls help.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

i guess u did not try my code

Function FormatMediumDate(DateValue)
    Dim strYYYY
    Dim strMM
    Dim strDD

      strYYYY = CStr(DatePart("yyyy", DateValue))
      strMM = CStr(DatePart("m", DateValue))
      If Len(strMM) = 1 Then strMM = "0" & strMM

      strDD = CStr(DatePart("d", DateValue))
      If Len(strDD) = 1 Then strDD = "0" & strDD

      FormatMediumDate = strDD& "-" & strMM & "-" & strYYYY
End Function

now u can use above function like...

Here's a function I wrote recently that allows you to format a date in a variety of ways, including the delimiter character.  I've also included some sample code at the bottom, to illustrate how to use it.

<%      '// Here are the following date formatting characters:
            '//            * MMMM = Month name ("January")
            '//            * MMM = First three letters of month name ("Jan")
            '//            * MM = Two-digit month number ("01")
            '//            * %M = One-digit month number ("1")
            '//            * dddd = Weekday name ("Wednesday")
            '//            * dd = Two-digit day number ("05")
            '//            * %d = One-digit day number ("5")
            '//            * yyyy = Complete year number ("2007")
            '//            * yy = Two right-most digits of year number ("07")
            Function CustomFormatDate(sDate, sDateFormat)
            Dim sReturnValue, dtDate, M, MM, MMM, MMMM, d, dd, dddd, yy, yyyy

            sReturnValue = ""
            if IsDate(sDate) then
                  dtDate = cDate(sDate)
                  M = Month(dtDate)
                  MM = M:  if (M < 10) then MM = "0" & MM
                  MMM = Left(MonthName(M), 3)
                  MMMM = MonthName(M)
                  d = Day(dtDate)
                  dd = d:  if (d < 10) then dd = "0" & dd
                  dddd = WeekdayName(Weekday(dtDate))
                  yy = Right(CStr(Year(dtDate)), 2)
                  yyyy = Year(dtDate)

                  sReturnValue = sDateFormat
                  sReturnValue = Replace(sReturnValue, "MMMM", MMMM,1,-1,0)
                  sReturnValue = Replace(sReturnValue, "MMM", MMM,1,-1,0)
                  sReturnValue = Replace(sReturnValue, "MM", MM,1,-1,0)
                  sReturnValue = Replace(sReturnValue, "%M", M,1,-1,0)
                  sReturnValue = Replace(sReturnValue, "dddd", dddd,1,-1,0)
                  sReturnValue = Replace(sReturnValue, "dd", dd,1,-1,0)
                  sReturnValue = Replace(sReturnValue, "%d", d,1,-1,0)
                  sReturnValue = Replace(sReturnValue, "yyyy", yyyy,1,-1,0)
                  sReturnValue = Replace(sReturnValue, "yy", yy,1,-1,0)
            end if

            CustomFormatDate = sReturnValue
      End Function

      Dim arrSampleDateFormats, dtSampleDate, iIndex, sSampleDateFormat, sFormattedDate
      arrSampleDateFormats = Array("%d-MMM-yyyy", "dd-MMM-yyyy", "MM/dd/yyyy", "MM.dd.yyyy", "MM-dd-yyyy", "dd/MM/yyyy", "dd.MM.yyyy", "dd-MM-yyyy", "dd/MMM/yy", "dd.MMM.yy", "dd-MMM-yy", "yyyy/MM/dd", "yyyy.MM.dd", "yyyy-MM-dd", "dddd, MMMM dd, yyyy", "MMMM dd, yyyy", "dddd, dd MMMM, yyyy", "dd MMMM, yyyy")
      dtSampleDate = CDate(#2007-09-01#) %>
      <div><font face="Courier" size="2">
            Sample Date = [<%=dtSampleDate%>]<br>

            <table width="100%">
<%      For iIndex = LBound(arrSampleDateFormats) to UBound(arrSampleDateFormats)
            sSampleDateFormat = arrSampleDateFormats(iIndex)
            sFormattedDate = CustomFormatDate(dtSampleDate, sSampleDateFormat) %>
                        <td width="250"><%=sSampleDateFormat%></td>
<%      Next %>

-= DeathToSpam =-
ah i missed...you need .net code
just try this one

Format(Date, "dd-MMM-yyyy")
no above date will not work. and this one will surely does wat u are expecting

Dim d As New Date

d = d.Now

Response.Write("<BR>" & Format(d, "dd-MMM-yyyy"))
kpankaj9Author Commented:
hey gawai..
cant belive it.....
its just worked by writing dd-MMM-yyyy.......thanks a lot
accepted ur solution...:)

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now