将 Java 日期转换为 Javascript 日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14330927/
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-10-26 21:19:31  来源:igfitidea点击:

Converting java Date into Javascript Date

javajavascriptjsondate

提问by Harish

I have a map.I am converting the Map into a JSON Object.I am reading the JSON object and want to convert it into a Javascript date.

我有一张地图。我正在将地图转换为 JSON 对象。我正在读取 JSON 对象并希望将其转换为 Javascript 日期。

The Date object I send is read as 2012-12-19T06:00:00.000+0000 in js and I do not understand what is the T in this String.Anyone can throw light on this

我发送的 Date 对象在 js 中被读取为 2012-12-19T06:00:00.000+0000,我不明白这个字符串中的 T 是什么。任何人都可以对此有所了解

回答by Arun P Johny

It is a string representation of a date as per the ISO 8601specification. Here Tstands for the beginning of the time portion of the datetime representation.

根据ISO 8601规范,它是日期的字符串表示形式。这里T代表日期时间表示的时间部分的开始。

You can convert this representation to javascript date object using new Date('2012-12-19T06:00:00.000+0000').

您可以使用new Date('2012-12-19T06:00:00.000+0000').

You can use a regex to get only the date portion. The regex /\d{4}-\d{2}-\d{2}/.exec('2012-12-19T06:00:00.000+0000')[0]will give you the date portion alone.

您可以使用正则表达式仅获取日期部分。正则表达式/\d{4}-\d{2}-\d{2}/.exec('2012-12-19T06:00:00.000+0000')[0]将单独为您提供日期部分。

Refer ISO 8601

参考 ISO 8601

回答by Bergi

The "T" is part of the ISO 8601 serialisation of the date, which is the JavaScript Date String Format.

“T”是日期的 ISO 8601 序列化的一部分,它是JavaScript 日期字符串格式

回答by Sumodh S

in java format the date to string the following simple date formatter

在 java 格式的日期字符串以下简单的日期格式化程序

SimpleDateFormat formatter = new SimpleDateFormat("EE MMM d y H:m:s ZZZ");
String dateString = formatter.format(new Date());

In java script side use

在java脚本端使用

new Date(dateString)