Link to home
Start Free TrialLog in
Avatar of Jagadeesh M
Jagadeesh MFlag for United States of America

asked on

Get Short Date Format using javascript

Is there anyway we can know using JavaScript the Short Date Format
used in the
Control Panel -> Regional and Language Settings?

I know the using the combinations of following we can get the Locale
Long Name format

toString()
toLocaleString()
toLocaleDateString()
toLocaleTimeString()

But there is no direct function in JavaScript like toLocaleShortDateString().

Are there any scripts available to find out what the user setting is. I would not like to use Applet, VBScript or ActiveX Control.

Thanks.
Avatar of HonorGod
HonorGod
Flag of United States of America image

Please define "short date format".

Given a date object does this qualify?

function d2( val ) {
  return ( val < 10 ) ? '0' + val : val;
}

var now = new Date();
alert( d2( now.getMonth() + 1 ) + '/' + d2( now.getDate() ) + '/' + d2( now.getYear() - 100 ) );
Avatar of Jagadeesh M

ASKER

Hi HonorGod -

I was expecting to hear from you. Thank you.

Check the below screenshot -

it is Control Panel -> Regional and Language Settings - Customize - Date !!


Short-Date-Format.gif
so basically my requirement is like this -

my database stores date always in mm/dd/yyyy format. But when i display the date to the user i should display it according their local system settings.

so if the user is having anything other than mm/dd/yyyy i should display it as dd/mm/yyyy.

Thank you,
hm.  I don't know how to retrieve the user specified locale specific date format.

:-(
should there be a way?
maybe.  I'll have to look ...  Hopefully we'll be able to find something shortly.
What do you get when you go to this page:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_tolocalestring

Do you get the expected/right output?
I think that it is the "long" date format, I'm still looking for the short form...
ASKER CERTIFIED SOLUTION
Avatar of HonorGod
HonorGod
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
yup i got the long date format!!
sorry
so can we conclude that it is not possible to obtain the short date format using javascript!
that is correct.
any other ideas on how this situation can be handled?

Cookies , ActiveX Objects etc?
Cookies, and their values have no relationship with system locale settings.
I've searched all over, and can find nothing that even hints that this is
possible.  The suggestion about an ActiveX object being able to do it was
a good one, but that search was also fruitless.

Sorry
ok.

At least can we interpret something like this -

When I run the below code I get "Wednesday, 25 June, 2008 ". When i get the date in this format (this is actually the long date format) i should it is in dd/mm/yyyy  

<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var curDateTime = new Date()
    document.write(curDateTime.toLocaleDateString());
//-->
</SCRIPT>

But when i run same code after changes my long date format in the control if I get "Wednesday, June 25, 2008 " i should be able to interpret as if it is in mm/dd/yyyy.

Is this possible.....it seems to be but have to play around with some regular expressions. Can you help me?
Well, I just tried it on my system, and see that IE respects the system locale
setting, but FireFox doesn't appear to.  What browser are you using?
I got lucky....it is just for IE!!
ok, after making the change to your locale setting, did you do a complete page reload (e.g., either closing your browser, and then restarting it, or holding the shift key down as you click on the reload icon), or did you just click on the reload icon?
i just refreshed the page and it picked the updated setting!
Ah.  Excellent.  So, are we set now?
no...i guessing i was asking in my earlier post on how to interpret / parse them to get the required way!!
The challenge is that you can get / create a Date object, and use the attribute "getters" to retrieve the different values (e.g., month, day of week, day of month, etc).  But "parsing" the locale specific date string is a bit of a challenge.

Are you doing this to generate a different date in the same locale specific format,
or what?
I guess we don't want to go by locale. I might be confusing you too much.

Now that Short Date Format is not possible to obtain in Javascript here is what i'm planning to do -

1. I have executed the below code and the response as "Monday, June 30, 2008 "
<SCRIPT Language="JavaScript">
<!-- hide from old browsers
  var curDateTime = new Date()
        document.write(curDateTime.toLocaleDateString());
//-->
</SCRIPT>
This based on the setting that Control Panel - Regional and Language Options (opens a dialog box) - from standards and formats section, the dropdown value is 'English (United State)' and the Long Date corresponding to this setting is the response i saw on my browser which is  "Monday, June 30, 2008 ".

So if see my long date in the above format i just wanted to interpret it as 'mm/dd/yyyy'.

2. Any other long date format, i just wanted to interpert as 'dd/mm/yyyy'.

Hope that helps.

So i guess the solution might be that if I see a date format "Monday, June 30, 2008 " i should be able to it is in 'mm/dd/yyyy' using regular expressions in java.....how can i get to this?






The problem with "parsing" a date of the form:

mm/dd/yyyy

is that you can't tell the difference between that, and one like:

dd/mm/yyyy

unless the date & month values are "outside of the normal" range.

To check to see if t a string contains one of these two, you can use something like:

var date = '7/2/2008';
if ( date.match( /(\d{1,2})\/(\d{1,2})\/(\d{4})$/ ) ) {
  alert( 'm=' + RegExp.$1 + '\nd=' + RegExp.$2 + '\nY=' + RegExp.$3 );
} else {
  alert( 'oops' );
}

Open in new window