问题描述
我希望能够转换Java日期格式字符串,例如 dd / MM / yyyy (07/06/2009)到JavaScript日期格式字符串,例如 dd / mm / yy (07/06/2009)。
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?
提前致谢。
编辑:
感谢所有回复,现在我意识到自己的错误,也可能是为什么你们这么多人都在努力理解这个问题; JavaScript没有内置日期格式化功能。我正在使用jQuery UI datepicker,我一直在设置它的日期格式,假设它在某个时候调用标准的JS函数,而不是使用它自己的库!当我搜索格式化字符串时,我直接跳到可以使用字母的表格,在开头跳过这一点来解释如何使用脚本。
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.
无论如何我我必须继续并且可能自己编写我想,将Java日期格式字符串转换为jQuery日期格式字符串(或尽可能接近) - 我正在研究我们产品的i18n并创建了一个存储的java类在整个应用程序中使用的首选日期格式字符串,我的意图是还能够为任何jsps提供JS中等效的格式字符串。
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.
无论如何,谢谢。
推荐答案
如果您只需要将日期从Java传递给JavaScript,我认为最好的方法就是要使用 date.getTime()
将Java日期转换为毫秒,请创建一个使用此毫秒值初始化的JavaScript日期,其中 new Date(毫秒)
然后格式化日期w使用JavaScript Date对象的方法,例如: date.toLocaleString()
。
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()
.
这篇关于Java日期格式为JavaScript日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!