views:

5420

answers:

8

Hi,

I would like to be able to convert a Java date format string, e.g. dd/MM/yyyy (07/06/2009) to a JavaScript date format string, e.g. dd/mm/yy (07/06/2009).

Has anyone done this before, or got any idea where I might find some code that already does this?

Thanks in advance.

Edit:

Thanks for all the replies but now I realise my mistake and possibly why so many of you were struggling to understand the question; JavaScript doesn't have a built in date formatting ability. I am using the jQuery UI datepicker and I have been setting its date format, assuming it would be calling a standard JS function at some point, not using its own library! When I googled for formatting strings I jumped straight to the tables of what letters could be used, skipping the bit at the begining explaining how to use the script.

Anyway I'll have to go ahead and possibly write my own I guess, converting a Java date format string into a jQuery date format string (or as close as possible) - I am working on the i18n of our product and have created a java class that stores the preferred date format string used throughout the application, my intention was to also have the abillity to supply any jsps with the format string that is equivalent in JS.

Thanks anyway.

+4  A: 

If you are using java, take a look at the Simple Date Format class.

Andrew Austin
+1  A: 

See this question, the answer of which also answers your question:

how to parse date in java?

matt b
A: 

The javascript code in this page implements some date functions and they "use the same format strings as the java.text.SimpleDateFormat class, with a few minor exceptions". It is not the very same as you want but it can be a good start point.

Csaba_H
+2  A: 

A similar topic has been answered here: Converting dates in JavaScript

I personally have found this to be a rather large pain and took the author's suggestion and used a library. As noted, jQuery datepicker has one that is a viable solution if you can afford the overhead of download for your application or already using it.

jnt30
I really think a libary is the way to go on this one as well. FWIW, dojo has a good one as well. http://docs.dojocampus.org/dojo/date/locale
seth
+2  A: 

If you just need to pass a date from Java to JavaScript, the best way to do it, I think, would be to convert the Java date to milliseconds using date.getTime(), create a JavaScript date initialized with this milliseconds value with new Date(milliseconds)and then format the date with the means of the JavaScript Date object, like: date.toLocaleString().

Ilya Boyandin
A: 

See this:

http://www.javascripttoolbox.com/lib/date/examples.php

Derrek
A: 

If you just want to format dates my date extensions will do that well - it also parses data formats and does a lot of date math/compares as well:

DP_DateExtensions Library

Not sure if it'll help, but I've found it invaluable in several projects.

Jim Davis
A: 

I created a jQuery plugin for formating java.util.Date.toString output using Javascript

http://github.com/phstc/jquery-dateFormat

//Text
$.format.date('2009-12-18 10:54:50.546', "dd/MM/yyyy");
//HTML Object
$.format.date($('#spanDate').text(), "dd/MM/yyyy");
//Scriptlet
$.format.date(<%=java.util.Date().toString()%>, "dd/MM/yyyy");
//JSON
var obj = ajaxRequest();
$.format.date(obj.date, "dd/MM/yyyy");
Pablo Cantero