admin管理员组

文章数量:1289483

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

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"

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.

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

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"

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.

Share Improve this question edited Nov 7, 2014 at 7:14 Mahib asked Jun 13, 2014 at 13:19 MahibMahib 4,0636 gold badges57 silver badges63 bronze badges 2
  • I've tried this >> new Date($("#InvoiceToDate").text()) But its showing >> Sat Jun 14 2014 00:00:00 GMT+0600 (Bangladesh Standard Time) – Mahib Commented Jun 13, 2014 at 13:28
  • 1 try like this: stackoverflow./questions/23532270/… – Uday Commented Jun 13, 2014 at 13:32
Add a ment  | 

2 Answers 2

Reset to default 4

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.

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.

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

See if this helps.

Something like this:

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

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

本文标签: javascriptHow to convert quotDDMMMYYYYquot string to dateStack Overflow