Click to See Complete Forum and Search --> : FormatDateTime equivalent in JavaScript


Conson
June 3rd, 2005, 04:15 AM
Hello Gurus!

I need to convert some client-side VBScript into Javascript.

In the VBScipt a date is converted from a weird format into the format that the user have set up on the client browser.
This is easily done by splitting the weird string into pieces and putting it back together with FormatDateTime.

I have created the Javascript to do the same BUT the problem is that I can only manage to get the long format of the date. I want the short!..
The VBScript function creates the short format as standard but can me set up to return the long etc.

Input looks like this "05-06-02" (yy-mm-dd).

The code looks like this:

function WDate2Date(e)
{
var timestamp, strday, strmonth, stryear, slash1, slash2, result;

result = e;
timestamp = e;
slash1 = timestamp.indexOf("-");
slash2 = timestamp.indexOf("-", slash1 + 1);
if(slash1 > -1 && slash2 > -1)
{
stryear = timestamp.substring(0, slash1);
strmonth = timestamp.substring(slash1+1, slash2);
strday = timestamp.substring(slash2+1, slash2+3);

if(parseInt(stryear) < 49)
{
stryear = parseInt(stryear) + 2000;
}
else
{
stryear = parseInt(stryear) + 1900;
}

var date = new Date(stryear, parseInt(strmonth) - 1, strday);
result = date.toLocaleString();
}
return result;
}


Have anyone got any workaround for this??
Or any other comment?

Thanks in advance!
Conson..

Dr. Script
June 3rd, 2005, 12:34 PM
I'd be happy to try to work it out. Please inform on the parameter, e (what does it expect to receieve in terms of type and value?). Currently, what value is returned from the function?

Conson
June 3rd, 2005, 03:08 PM
Hey Dr. Script.
Thanks a lot for your reply!!

e is supposed to be a string input. As I mentioned in the original post it is supposed to have the following layout: "05-06-02" (year-month-date).
The returned string from the function in my post is identical to the one set up in the control panel (regional options) to be a long date.
It gives name of day, date, month in written text and so on.
I just want the short version as set up in the control panel.
The script is supposed to be used on client machines in different countries so I cannot just hard-code it in some format..

When the date stuff is up and running I also have to insert time but that should be a piece of cake...

I've realised that if I use toLocaleDateString instead of toLocaleString I don't get the name of the current day and time. But I still have the month in written text....

Am I making sense??

Thanks again!
Conson..

Dr. Script
June 3rd, 2005, 05:15 PM
I think I do understand. However, I'm not positive how to go about changing that. What you can do (what I would do), is create the short date just as yy-mm-dd. Then, use VBScript to change this variable using the FormatDateTime function. VBscript is IE only, so the IE users will get the VBScript display. Other browsers will ignore the VBScript, and display the yy-mm-dd format. If that is OK, then I can code that for you ...

Conson
June 4th, 2005, 03:59 PM
Hey again Dr. Script..

Thanks for reply..

I just hoped there were an easy solution for this.. But it looks like there are no easy way..

I've been a "spokesman" for javascipt rather than VBScript at my work.. But it looks like the VBScript people have received a point in this match.. :(
At the moment we have this function running in VBScript.. I was just hoping to convert it to javascript when adding new functionality to the page..

Thanks a lot for your time and comments!
Conson

Dr. Script
June 4th, 2005, 04:03 PM
What are the parameters of the FormatDateTime function? IF you give me them, then I'll write a code that would allow the conversion for IE users.

Conson
June 4th, 2005, 04:22 PM
Hey again!

There is no need for you spending time with it.. We have the function implemented in VBScript at the moment..
(I'm not at work right now so I cannot show it to you..)

But thanks anyway!

As far as I can remember you just give the function a date object and it returns a short date + time in a string. A optional parameter enables to change the format..

Conson.