Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date format convert javascript

I am trying to convert "July 24 2013" to "DD-MM-YYYY" with javascript but I keep getting and error.

I am using new Date('July 24 2013').format("DD-MM-YYYY");

What am I missing?

like image 573
user2597255 Avatar asked Jul 18 '13 21:07

user2597255


People also ask

What date format is DD MMM YYYY JavaScript?

There is no native format in JavaScript for” dd-mmm-yyyy”. To get the date format “dd-mmm-yyyy”, we are going to use regular expression in JavaScript.

How do I change the date format in a new date?

const d = new Date("2015-3-25"); The behavior of "YYYY/MM/DD" is undefined.

What is toISOString in JavaScript?

toISOString() The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ , respectively).


2 Answers

Date object doesn't have .format() method. If you work with date and time actively I'd recommend you to use MomentJS library then. It has a lot of useful functionality to work with time and date.

For example: moment('July 24 2013', 'MMMM D YYYY').format('MMMM D YYYY, h:mm:ss a');

like image 89
Eugene Naydenov Avatar answered Oct 14 '22 07:10

Eugene Naydenov


I would highly advise using momentjs!

moment('July 24 2013').format("DD-MM-YYYY");
// => "24-07-2013"

Simple as that!

like image 34
Mulan Avatar answered Oct 14 '22 09:10

Mulan