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

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
0
DesertWarrior
Asked:
DesertWarrior
1 Solution
 
masirofCommented:
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
0
 
peh803Commented:
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
0
 
raj3060Commented:
Refer the link:
http://www.psacake.com/web/fr.asp
-Raj
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

peh803
0
 
aprestoCommented:
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
0
 
kmorris1186Commented:
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.
0
 
DesertWarriorAuthor Commented:
Sorry guys, I forgot to mention : I work only with javascript...

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

I use MySQL and dont have that luxary..
0
 
Brad Dobyns, CSMCommented:
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));
      emonth--;
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,
Brad
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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