Java 日期格式转 JavaScript 日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1007732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:12:29  来源:igfitidea点击:

Java date format to JavaScript date format

javajavascriptdate-format

提问by Ed .

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

我希望能够将 Java 日期格式字符串(例如dd/MM/yyyy(07/06/2009))转换为 JavaScript 日期格式字符串,例如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.

感谢您的所有回复,但现在我意识到我的错误以及可能为什么你们中的许多人都在努力理解这个问题;JavaScript 没有内置的日期格式化功能。我正在使用 jQuery UI datepicker 并且我一直在设置它的日期格式,假设它会在某个时候调用一个标准的 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 日期格式字符串转换为 jQuery 日期格式字符串(或尽可能接近)-我正在研究我们产品的 i18n 并创建了一个 java类存储整个应用程序中使用的首选日期格式字符串,我的目的是还能够提供任何 jsps 与 JS 中等效的格式字符串。

Thanks anyway.

不管怎么说,还是要谢谢你。

回答by Andrew Austin

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

如果您使用的是 java,请查看Simple Date Format类。

回答by Csaba_H

The javascript code in this pageimplements 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.

此页面中的 javascript 代码实现了一些日期函数,它们“使用与 java.text.SimpleDateFormat 类相同的格式字符串,但有一些小例外”。它与您想要的不太一样,但它可以是一个很好的起点。

回答by jnt30

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

这里已经回答了一个类似的主题: Converting date 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.

我个人发现这是一个相当大的痛苦,并接受了作者的建议并使用了一个库。如前所述,jQuery datepicker 有一个可行的解决方案,如果您负担得起应用程序的下载开销或已经在使用它。

回答by Ilya Boyandin

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,我认为最好的方法是使用 将 Java 日期转换为毫秒date.getTime(),创建一个使用此毫秒值初始化的 JavaScript 日期new Date(milliseconds),然后使用JavaScript的Date对象,像手段:date.toLocaleString()

回答by Jim Davis

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

DP_DateExtensions 库

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

不确定它是否会有所帮助,但我发现它在几个项目中非常宝贵。

回答by Pablo Cantero

You could use my plugin jquery-dateFormat.

你可以使用我的插件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");

回答by JaXoN

This JavaScript library should be able to help you.

这个 JavaScript 库应该能够帮助你。

http://plugins.jquery.com/project/fIsForFormat

http://plugins.jquery.com/project/fIsForFormat

(I don't know why they have it as a jQuery Plugin, because it works standalone.)

(我不知道他们为什么把它作为 jQuery 插件,因为它可以独立工作。)

You'd simply split the original formatted date into its individual elements and then create a new Date Object with those elements. Then, use this library's "Date.f()" method to output it into any format you could want.

您只需将原始格式化日期拆分为其各个元素,然后使用这些元素创建一个新的日期对象。然后,使用该库的“Date.f()”方法将其输出为您想要的任何格式。

For example:

例如:

var dateOld = "11/27/2010",
    dateArr = date1.split("/"),
    dateObj = new Date(dateArr[2], dateArr[0], dateArr[1]),
    dateNew = dateObj.f("MMM d, yyyy");

document.write("Old Format: " + dateOld + "<br/>New Format: " + dateNew);

回答by Naveed Kamran

This works fine for me:

这对我来说很好用:

<%
Date date = Calendar.getInstance().getTime();
%>

<script>
var d = new Date(<%=date.getTime()%>);
alert(d);
</script>

回答by Hristo

Check out moment.js! It's "A lightweight javascript date library for parsing, manipulating, and formatting dates". It is a really powerful little library.

看看moment.js!它是“用于解析、操作和格式化日期的轻量级 JavaScript 日期库”。这是一个非常强大的小图书馆。

Here's an example...

这是一个例子...

var today = moment(new Date());
today.format("MMMM D, YYYY h:m A");        // outputs "April 11, 2012 2:32 PM"

// in one line...
moment().format("MMMM D, YYYY h:m A");     // outputs "April 11, 2012 2:32 PM"

Here's another example...

这是另一个例子...

var a = moment([2012, 2, 12, 15, 25, 50, 125]);
a.format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Monday, March 12th 2012, 3:25:50 pm"
a.format("ddd, hA");                       // "Mon, 3PM"
a.format("D/M/YYYY");                      // "12/3/2012"


Also, its worth mentioning to checkout date.js. I think the two libraries complement each other.

此外,值得一提的是结帐date.js。我认为这两个库是相辅相成的。

回答by André Vinícius

I suggest you the MomentJSwith this Pluginthat allow you to convert a Java pattern to a JS pattern (MomentJS)

我建议您使用带有此插件MomentJS,它允许您将 Java 模式转换为 JS 模式(MomentJS)