简体   繁体   中英

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

您可以在Date对象上使用toDateString()方法。

arr.push(next.toDateString());

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());
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM