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.
LVL 10
jagadeesh_motamarriAsked:
Who is Participating?
 
HonorGodConnect With a Mentor Software EngineerCommented:
Unfortunately, all references that I find indicate that only the "long" form of the locale date string are available.  :-(
0
 
HonorGodSoftware EngineerCommented:
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 ) );
0
 
jagadeesh_motamarriAuthor Commented:
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
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
jagadeesh_motamarriAuthor Commented:
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,
0
 
HonorGodSoftware EngineerCommented:
hm.  I don't know how to retrieve the user specified locale specific date format.

:-(
0
 
jagadeesh_motamarriAuthor Commented:
should there be a way?
0
 
HonorGodSoftware EngineerCommented:
maybe.  I'll have to look ...  Hopefully we'll be able to find something shortly.
0
 
HonorGodSoftware EngineerCommented:
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?
0
 
HonorGodSoftware EngineerCommented:
I think that it is the "long" date format, I'm still looking for the short form...
0
 
jagadeesh_motamarriAuthor Commented:
yup i got the long date format!!
0
 
HonorGodSoftware EngineerCommented:
sorry
0
 
jagadeesh_motamarriAuthor Commented:
so can we conclude that it is not possible to obtain the short date format using javascript!
0
 
HonorGodSoftware EngineerCommented:
that is correct.
0
 
jagadeesh_motamarriAuthor Commented:
any other ideas on how this situation can be handled?

Cookies , ActiveX Objects etc?
0
 
HonorGodSoftware EngineerCommented:
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
0
 
jagadeesh_motamarriAuthor Commented:
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?
0
 
HonorGodSoftware EngineerCommented:
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?
0
 
jagadeesh_motamarriAuthor Commented:
I got lucky....it is just for IE!!
0
 
HonorGodSoftware EngineerCommented:
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?
0
 
jagadeesh_motamarriAuthor Commented:
i just refreshed the page and it picked the updated setting!
0
 
HonorGodSoftware EngineerCommented:
Ah.  Excellent.  So, are we set now?
0
 
jagadeesh_motamarriAuthor Commented:
no...i guessing i was asking in my earlier post on how to interpret / parse them to get the required way!!
0
 
HonorGodSoftware EngineerCommented:
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?
0
 
jagadeesh_motamarriAuthor Commented:
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?






0
 
HonorGodSoftware EngineerCommented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.