format date

Posted on 2007-10-08
Last Modified: 2008-01-09
Hi.. Can anybody tell me how can I format my datetime object like eg  1-Jan-2013
Question by:kpankaj9
    LVL 18

    Expert Comment

    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.
    LVL 20

    Expert Comment


    Author Comment

    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.

    LVL 20

    Expert Comment

    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...

    LVL 9

    Expert Comment

    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 =-
    LVL 20

    Expert Comment

    ah i need .net code
    LVL 20

    Expert Comment

    just try this one

    Format(Date, "dd-MMM-yyyy")
    LVL 20

    Accepted Solution

    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"))

    Author Comment

    hey gawai..
    cant belive it.....
    its just worked by writing dd-MMM-yyyy.......thanks a lot
    accepted ur solution...:)

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now