PDA

View Full Version : Need HELP on displaying Regional Date settings


dimension
11-06-2003, 11:00 AM
Hi, I' m really stuck in this situation, your assistance would be GREATLY appreciated...

Well this is the problem, basically i need to display the Clients date using javascript. I'm supposed to extract it from the regional settings on the machine and format it to short format, eg. dd/mm/yy.

However it should be generic, that is if the date is to be shown in UK is would be dd/mm/yy,
if it was for US it would be mm/dd/yy,
if it was say for Norway it would be dd.mm.yy

I'm not supposed to hard code any of the days, months or seperators, should be generic, basically should be taken off the system date.

I feel this is quite difficult but a good challenge. Here is the code that i have achieved so far.

<html>
<body>
<head>
<script type="text/javascript">

var myDate = new Date()
var regionalDate = myDate.toLocaleDateString()
document.write(regionalDate)

</script>
</head>
</body>
</html>

A suggestion to see if it works, goto Control Panel and click on Regional Settings, and click the drop down menu and change the country. I'm try to use this in Norway as well as in States and UK that is why i need all those formats, however not hard coded.

Thank you very much :)

Kor
11-06-2003, 11:59 AM
4 solutions I see:

1. Write the short name for month . It woun't matther now if 27/IAN/03 or IAN/27/03 . People will understand easy.

2. make a variable from the user's hour getHours

if it's hours match between the American East ans West Coast, display American way, else European. Still remains the UK-Norway difference.... If so, you may combined with next solution, #3

3. Try to get the country's code from the client's IP. Still there will the .com or other impersonal domains, never knows which country are...

4. Ask client which format will prefere . Use a cookie not to bother later with that question...

Kor
11-06-2003, 12:07 PM
...and 5 (in fact 4.2 :)

insert small buttons and invite client to choose the proper display of the date. Optional, use cookie to display his way next enter

Roelf
11-06-2003, 12:59 PM
perhaps this could be of use:
http://www.devguru.com/Technologies/ecmascript/quickref/date_tolocalestring.html

i dont know if the separator issue is solved with this, but is should help you a few steps forward

dimension
11-07-2003, 02:44 AM
Thanks people.. but do you'll think it is possible for it to change automatically, respectively?? like say if i'm in Norway it'll be dd.mm.yy and if i'm in States it'll be mm/dd/yy??

Also is there any other way to get the date, month and year through the toLocaleDateString()?? or is the only way like

var myDate = new Date()

var date = myDate.getDate()

var month = myDate.getMonth()

Thanks again for your help :D

:thumbsup:

dimension
11-07-2003, 04:39 AM
Oka could you please explain to me how i could do this?? no. 2

2. make a variable from the user's hour getHours

if it's hours match between the American East ans West Coast, display American way, else European. Still remains the UK-Norway difference.... If so, you may combined with next solution, #3

and also how i could get the country's code from the client's IP??

3. Try to get the country's code from the client's IP. Still there will the .com or other impersonal domains, never knows which country are...

Kor
11-07-2003, 07:54 AM
1. - You can grab the user's computer hour using getHours() method and the GMT Offset using getTimezoneOffset() method:

<script>
var date = new Date();
var hour = date.getHours();
var GMToffset = date.getTimezoneOffset()/60;
alert('hour is '+ hour + ' and GMT offset is ' + GMToffset);
</script>

Now what you really need is the GMT Offset.

2. - To grab the IP it's a more intricate thing, and it can be done only using a combination between a server-side application (Java, SSI, php...) and Javascript

dimension
11-07-2003, 08:03 AM
Thanks, will try that, but was trying another solution. Do u know how to get the locale from the Regional Settings? Cos i have realised that i have to hard code some thing, so i have like some countries, 'en' for US, 'en_GB' for UK, 'no' for Norway. These are with their respective formats like the -, ., / So if i can get the locale from the system then i have to only compare it with the ones in the source code and it will be display in that format.

Thanks for your help :D

Kor
11-07-2003, 08:49 AM
hm... there is a method who can compare a string to the regional settings way to display a string.... It calls toLocaleString() and returns a certain string according to the local/regional settings ... But I never use that... I shall think how can this me be useful to you

dimension
11-07-2003, 08:53 AM
Hey thanks but i think i figured a way out. I used the language command

navigator.userLanguage

this uses the system settings, so it seems to work.

Thanks... :cool:

Kor
11-07-2003, 09:59 AM
Yes... I knwe that but i figured that will not help you too much... For instance if you will check my PC you will se that my userLanguage is US English, not Romanian, even I could switch easily to... But if you say it is good for you...


Anyway, I tested and I think that method toLocaleString it might help you also:

For instance:

<SCRIPT Language="JavaScript">

var date = new Date();
var hour = date.getHours();
var GMToffset = date.getTimezoneOffset()/60;
var localdate = date.toLocaleString();
alert('Local settings = '+localdate+' --- '+'Default JS ='+' '+date);

</SCRIPT>

Now You must presume that, nomatter what userLanguage is setted, the user had set his own way to display the date, and the script above returns exactly that value, I mean the way the user had set his regional date display... It might be useful also