Behseini
Behseini

Reputation: 6320

How to Format JavaScript Long String Date

I am using this code to export dates of current week days

var arr =[];
var curr = new Date(); 
var first = curr.getDate() - curr.getDay();
for (var i = 1; i < 6; i++) {
    var next = new Date(curr.getTime());
    next.setDate(first+1 );
    arr.push(next.toString());

}

but the output looks like Mon Nov 09 2015 01:43:57 GMT-0800 (Pacific Standard Time) in the array of

["Mon Nov 09 2015 01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015 01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015 01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015 01:43:57 GMT-0800 (Pacific Standard Time)", "Mon Nov 09 2015 01:43:57 GMT-0800 (Pacific Standard Time)"]

Can you please let me know how I can format the date() to get only Mon Nov 09 2015 and remove 01:43:57 GMT-0800 (Pacific Standard Time)?

Thanks

Upvotes: 0

Views: 137

Answers (3)

Kerwin
Kerwin

Reputation: 1212

document.write(new Date().toDateString());

Upvotes: 1

Azad
Azad

Reputation: 5264

Its simple. I am using your code. just use toDateString() method of Date()

var arr =[];
var curr = new Date(); 
var first = curr.getDate() - curr.getDay();
for (var i = 1; i < 6; i++) {
    var next = new Date(curr.getTime());
    next.setDate(first+1 );
    arr.push(next.toDateString());
}

Upvotes: 1

laszlokiss88
laszlokiss88

Reputation: 4071

You can use the toDateString() method on the Date object.

arr.push(next.toDateString());

Upvotes: 2

Related Questions