Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java date format to JavaScript date format

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 realize 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 beginning 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 ability to supply any jsps with the format string that is equivalent in JS.

Thanks anyway.

like image 585
Ed . Avatar asked Jun 17 '09 15:06

Ed .


People also ask

How do I format a date in JavaScript?

The JavaScript toDateString() method returns the date portion of a date object in the form of a string using the following format: First three letters of the week day name. First three letters of the month name. Two digit day of the month, padded on the left a zero if necessary.

What date format is DD MMM YYYY JavaScript?

There is no native format in JavaScript for” dd-mmm-yyyy”. To get the date format “dd-mmm-yyyy”, we are going to use regular expression in JavaScript. The regular expression in JavaScript is used to find the pattern in a string. So we are going to find the “dd-mmm-yyyy” pattern in the string using match() method.

Can we use SimpleDateFormat in JavaScript?

The format method is used to format a given date based on the defined pattern. The resulting formatted string is returned. var mySimpleDateFormatter = new simpleDateFormat('MMMM d, yyyy'); var myDate = new Date('2012', '07', '10'); document. getElementById('myDateContainer').

How do you change date format to MM DD YYYY in Java?

You can just use: Date yourDate = new Date(); SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); String date = DATE_FORMAT. format(yourDate);


2 Answers

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().

like image 57
Ilya Boyandin Avatar answered Sep 18 '22 18:09

Ilya Boyandin


You could use my plugin 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"); 
like image 31
Pablo Cantero Avatar answered Sep 18 '22 18:09

Pablo Cantero