Solved

Get Short Date Format using javascript

Posted on 2008-06-24
27
4,913 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:jagadeesh_motamarri
  • 14
  • 11
27 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 21856762
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
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 21858980
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
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 21859005
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 21859457
hm.  I don't know how to retrieve the user specified locale specific date format.

:-(
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 21859521
should there be a way?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21859804
maybe.  I'll have to look ...  Hopefully we'll be able to find something shortly.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21860322
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 21860337
I think that it is the "long" date format, I'm still looking for the short form...
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 21860424
Unfortunately, all references that I find indicate that only the "long" form of the locale date string are available.  :-(
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 21860434
yup i got the long date format!!
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21860441
sorry
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 21860515
so can we conclude that it is not possible to obtain the short date format using javascript!
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 41

Expert Comment

by:HonorGod
ID: 21860669
that is correct.
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 21862038
any other ideas on how this situation can be handled?

Cookies , ActiveX Objects etc?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21862143
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
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 21866651
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 21866914
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
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 21867089
I got lucky....it is just for IE!!
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21867405
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
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 21867457
i just refreshed the page and it picked the updated setting!
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21867571
Ah.  Excellent.  So, are we set now?
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 21869226
no...i guessing i was asking in my earlier post on how to interpret / parse them to get the required way!!
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21869295
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
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 21901436
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 21917956
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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now