Link to home
Start Free TrialLog in
Avatar of DesertWarrior
DesertWarrior

asked on

Format date DD/MM/YYYY

Hi,
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
Avatar of masirof
masirof

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
Avatar of peh803
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!
peh803
raj3060 -- good post; does exactly what mine does, except in a function.  Useful for sure!

peh803
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
Here is a DLL that i use that will allow you to format dates and times just like you would in VB..

http://www.4guysfromrolla.com/demos/FormatDemo.asp

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

ASKER

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

peh803, can you translate that code into javascript?
There seems to be a good js formatDate function here:
http://www.mattkruse.com/javascript/date/compact_source.html
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));
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)"
That is, only if they are using MSSQL...

I use MySQL and dont have that luxary..
ASKER CERTIFIED SOLUTION
Avatar of Brad Dobyns, CSM
Brad Dobyns, CSM
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial