在 react/javascript 中转换字符串日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50430968/
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
Converting string date in react/javascript
提问by bruce
I'm getting a string date from a JSON object. I need to convert the date
我从 JSON 对象获取字符串日期。我需要转换日期
"2018-05-18T04:00:00.000Z"to. May 18 , 2018
"2018-05-18T04:00:00.000Z"到。 May 18 , 2018
I tried using the Date() constructor but I wasn't successful.
我尝试使用 Date() 构造函数,但没有成功。
I've heard of Intl.DateTimeFormatbut I don't understand the documentation. Any help and explanation is appreaiated .
我听说过,Intl.DateTimeFormat但我不明白文档。任何帮助和解释appreaiated 。
采纳答案by ninjaAF
You should be able to do this purely with JavaScript, including parsing with Date() and, depending on your browser support requirements, format with toLocaleDateString().
您应该能够完全使用 JavaScript 执行此操作,包括使用 Date() 进行解析,并且根据您的浏览器支持要求,使用toLocaleDateString() 进行格式化。
The second example (commented out) just shows how it can work with a specific locale (as a string, though that can be an array of such strings).
第二个示例(已注释掉)仅显示了它如何与特定语言环境一起工作(作为字符串,但可以是此类字符串的数组)。
function formatDate(string){
var options = { year: 'numeric', month: 'long', day: 'numeric' };
return new Date(string).toLocaleDateString([],options);
}
var dateString = "2018-05-18T04:00:00.000Z"
document.getElementById("results").innerHTML = formatDate(dateString);
// You could also provide specific locale, if needed. For example:
//function formatDate(string){
// var options = { year: 'numeric', month: 'long', day: 'numeric' };
// return new Date(string).toLocaleDateString('en-US',options);
//}
<div id="results"</div>
回答by pjb
回答by Randy Casburn
You've created a new Date object correctly and JavaScript stored the correct date. The issue has to do with Locale. In this next line of code, JS has created and stored that exact time. But when we ask for an output from JS we usually get exactly what we ask. From you're example, you've asked for the date based upon a locale that is most likely in the USA. So this:
您已经正确地创建了一个新的 Date 对象并且 JavaScript 存储了正确的日期。问题与语言环境有关。在下一行代码中,JS 已经创建并存储了那个确切的时间。但是当我们要求 JS 的输出时,我们通常会得到我们所要求的。从你的例子来看,你已经根据最有可能在美国的语言环境要求日期。所以这:
new Date('2018-05-18T04:00:00Z').toLocaleString(); //(or .toString()
will give you exactly the result you ask, which is a UTC time shifted to a different locale (I've left the time on for demonstration).
将为您提供您所要求的准确结果,即移至不同语言环境的 UTC 时间(我已留出时间进行演示)。
On the other hand, using this:
另一方面,使用这个:
new Date('2018-05-18T04:00:00Z').toUTCString();
Will produce the result you expect because it enforces the correct locale to be UTC.
将产生您期望的结果,因为它将正确的语言环境强制为 UTC。
document.querySelector('#a').innerText = new Date('2018-05-18T04:00:00Z').toString();
document.querySelector('#b').innerText = new Date('2018-05-18T04:00:00Z').toUTCString();
Incorrect: <p id="a"></p>
<hr>
Correct: <p id="b"></p>
回答by Hiru Piyumika
function formatDate(string){
var options = { year: 'numeric', month: 'long', day: 'numeric' };
return new Date(string).toLocaleDateString([],options);
}
var dateString = "2018-05-18T04:00:00.000Z"
document.getElementById("results").innerHTML = formatDate(dateString);
// You could also provide specific locale, if needed. For example:
//function formatDate(string){
// var options = { year: 'numeric', month: 'long', day: 'numeric' };
// return new Date(string).toLocaleDateString('en-US',options);
//}
<div id="results"</div>
回答by user9483138
I think what you need to do is:
我认为你需要做的是:
let date = new Date(string);
让日期=新日期(字符串);
let readableDate = date.toDateString();
让 readableDate = date.toDateString();

