Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get exact day from date string in Javascript

I have checked this SO post: Where can I find documentation on formatting a date in JavaScript?

Also I have looked into http://home.clara.net/shotover/datetest.htm

My string is: Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)

And I want to convert it to dd-mm-yyyy format.

I tried using:

var dateString = 'Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)';
var myDate = new Date(dateString);
var final_date = myDate.getDay()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();

But it gives me the result as: 1-6-2013

The getDay() value is the index of day in a week.
For Instance, If my dateString is Thu Jun 20 2013 05:30:00 GMT+0530 (India Standard Time)
it gives output as 4-6-2013

How can I get the proper value of Day?

P.S: I tried using .toLocaleString() and creating new date object from it. But it gives the same result.

like image 597
Prasad Jadhav Avatar asked Jun 11 '13 06:06

Prasad Jadhav


1 Answers

To get the day of the month use getDate():

var final_date = myDate.getDate()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();
like image 51
Sirko Avatar answered Sep 28 '22 07:09

Sirko