anukuls
anukuls

Reputation: 285

Cannot parse Date into different format

I have date in String as Wed Sep 30 18:37:04 IST 2015 I want to convert it into 09-30-2015 format.

I have tried

var dateString = "Wed Sep 30 18:37:04 IST 2015";
var d = new Date(dateString);

But it is giving invalid date.

Upvotes: 1

Views: 112

Answers (2)

Gagan Jaura
Gagan Jaura

Reputation: 719

There is no built in function for formatting date in jQuery. Either you have to use jquery plugins or do it manually. For JQuery plugin you can use this Link

For manual use below code:

$(document).ready(function(){    
    var d = new Date(); 
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();
    alert(curr_date + "-" + curr_month + "-" + curr_year);
});

Upvotes: 0

Ionică Bizău
Ionică Bizău

Reputation: 113505

For this, you don't even need to use the Date constructor. You can manipulate the string like this:

var input = "Wed Sep 30 18:37:04 IST 2015";
var splits = input.split(" ");
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var finalDate = (months.indexOf(splits[1]) + 1) + "-" + splits[2] + "-" + splits[5].slice(-2);

alert(finalDate);

Upvotes: 1

Related Questions