javascript 如何将“DD-MMM-YYYY”字符串转换为日期

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

How to convert "DD-MMM-YYYY" string to date

javascriptjquerydate

提问by Mahib

How to convert a "DD-MMM-YYYY" string to a date in Jquery?

如何在 Jquery 中将“DD-MMM-YYYY”字符串转换为日期?

I've a date in string "14-Jun-2014". I want to convert it to a date; something like this 14-06-2014. For $("#InvoiceToDate").text();I get this "14-Jun-2014"

我在字符串“14-Jun-2014”中有一个日期。我想将其转换为日期;像这样的东西14-06-2014。因为$("#InvoiceToDate").text();我得到这个"14-Jun-2014"

So for

因此对于

new Date($("#InvoiceToDate").text()) 

But its showing

但它的显示

Sat Jun 14 2014 00:00:00 GMT+0600 (Bangladesh Standard Time)

I've also tried this

我也试过这个

new Date($("#InvoiceToDate").text()).toLocaleString('en-GB');

Now its showing almost what I wanted.

现在它几乎显示了我想要的东西。

"06/08/2014 00:00:00"

How can I get only dd/MM/yyyy. Please help.

我怎样才能只得到 dd/MM/yyyy。请帮忙。

回答by Hackerman

Something like this:

像这样的东西:

var fecha = "14-Jun-2014";
var myDate = new Date(fecha);
console.log(myDate);

Fiddle: http://jsfiddle.net/robertrozas/pS8tc/1/

小提琴:http: //jsfiddle.net/robertrozas/pS8tc/1/

回答by Siddik

I had similar kind of problem. This works for me.

我有类似的问题。这对我有用。

var now = new Date("14-Jun-2014");
now.toLocaleDateString("en-GB");

This returns "14/06/2014" in Chrome.

这将在 Chrome 中返回“14/06/2014”。

UPDATE:

更新:

FF and IE wouldn't swallow "14-Jun-2014". However, if you could remove the dashes from the string and replace with spaces like "14 Jun 2014" then it will work on both browsers.

FF 和 IE 不会吞下“14-Jun-2014”。但是,如果您可以从字符串中删除破折号并替换为“2014 年 6 月 14 日”之类的空格,那么它将适用于两种浏览器。

var now = new Date("14 Jun 2014");
now.toLocaleDateString("en-GB");

See if this helps.

看看这是否有帮助。