Format date DD/MM/YYYY

Posted on 2004-11-09
Last Modified: 2012-05-05
I'm getting a date from a table, and then I want to save it to another table, but I want to save the date in the format dd/mm/yyyy... how do I do that? I don't want to get any 5/5/2004... I want to get 05/05/2004. Thanks for your help

here's my code :

var DateTemp = new Date()
DateTemp = ""+rsGetBGPData("effective_Date")
strFillBgar += "FromDate = '" + DateTemp + "', "  //I build all my SQL statement in the strFillBgar variable then I execute it at the end
Question by:DesertWarrior
    LVL 6

    Expert Comment

    First split into day month year
    and check by LEN if its 2 digits or not, like
    if Len(varDAY) = 1 Then varDAY = "0" & varDAY

    and join them together :D
    LVL 19

    Expert Comment

    var DateTemp = new Date()

    This looks like javascript; if you want to use vbscript, do it like this (this is just masirof's idea, spelled out a bit further)

    Dim sMyDate   ' m/d/yyyy format
    Dim sNewDate  ' mm/dd/yyyy format
    Dim sDay
    Dim sMonth
    sMyDate = "4/1/2000"
    sDay = day(sMyDate)
    sMonth = month(sMyDate)

    if len(sDay)=1 then
      sDay = "0"&sDay
    end if

    if len(sMonth)=1 then
      sMonth = "0"&sMonth
    end if

    sNewDate = sMonth & "/" & sDay & "/" & year(sMyDate)
    response.write "Day before conversion: "&sMyDate
    response.write "Day after conversion: "&sNewDate


    Hope this helps!
    LVL 11

    Expert Comment

    Refer the link:
    LVL 19

    Expert Comment

    raj3060 -- good post; does exactly what mine does, except in a function.  Useful for sure!

    LVL 23

    Expert Comment

    this is a very lame suggestion compared to other but would it work if you put this neat the top of your code:

    <% session.lcid = 2057 %>

    but that might just display them in british date format
    LVL 7

    Expert Comment

    Here is a DLL that i use that will allow you to format dates and times just like you would in VB..

    It is freeware, but is an active X dll.  If your trying to stay away from that, this will not help.
    LVL 5

    Author Comment

    Sorry guys, I forgot to mention : I work only with javascript...

    peh803, can you translate that code into javascript?
    LVL 19

    Expert Comment

    There seems to be a good js formatDate function here:
    LVL 19

    Expert Comment

    Or, how about this?

    function getDisplayDate(passedDate) {
        // MM/DD/YYYY

        theDate = new Date(passedDate);

        // split into day, month, year
        iDay = theDate.getDate();
        iMonth = theDate.getMonth();
        iYear = theDate.getFullYear();

        sDisplayDate = iMonth + "/" + iDay + "/" + sYear;

        return sDisplayDate;

    // example use
    now = new Date();
    document.writeln("Today's Date is: " + getDisplayDate(now));

    Expert Comment

    You can try SQL convert function with style to do this easily

    E.g. select convert(varchar, convert(datetime,('5/5/2004')), 103)
    will give you "05/05/2004"

    So, use
    strFillBgar += "FromDate = convert(varchar, convert(datetime,('" + DateTemp + "')), 103)"
    LVL 7

    Expert Comment

    That is, only if they are using MSSQL...

    I use MySQL and dont have that luxary..
    LVL 7

    Accepted Solution

    Not sure that that will include the "0" in front of the numbers like he wants. You have to include a conditional:

    if (iDay< 10) iDay = ("0" + iDay + ", ");
    if (iMonth< 10) IMonth = ("0" + iMonth + ", ");

    Try this (this is what I use):

    var calendar = new Date();
    var iMonth = calendar.getMonth();
    var iDate = calendar.getDate();
    var iYear = calendar.getYear();
    if (iYear < 1000)
          iYear += 1900;
    var cent = parseInt(iYear/100);
    var g = iYear % 19;
    var k = parseInt((cent - 17)/25);
    var i = (cent - parseInt(cent/4) - parseInt((cent - k)/3) + 19 * g + 15) % 30;
          i -= parseInt(i/28) * (1 - parseInt(i/28) * parseInt(29/(i+1)) * parseInt((21-g)/11));
    var j = (IYear + parseInt(iYear/4) + i + 2 - cent + parseInt(cent/4)) % 7;
    var l = i - j;
    var emonth = 3 + parseInt((l + 40)/44);
    var edate = l + 28 - 31 * parseInt((emonth/4));
    if (iMonth< 10)
         iMonth = ("0" + iMonth + ", "); // if you want leading zero on month
    if (iDate< 10)
        iDate = ("0" + iDate + "/"); // if you want leading zero on date
    DateTemp = IMonth + IDate + IYear;

    This takes into account leap year and February.

    Hope that helps,

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video discusses moving either the default database or any database to a new volume.

    734 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

    17 Experts available now in Live!

    Get 1:1 Help Now