Javascript从日期中删除日期名称

Mag*_*zad 8 javascript jquery date

我想问一下是否有人知道如何从以下示例中删除日期名称,警报返回 2020 年 2 月 29 日星期六,我不使用 Moment.js 仅 Jquery,因为我只需要能够以以下格式处理日期下面写成代码。

var mydate = new Date('29 Feb 2020');
alert(mydate.toDateString());
Run Code Online (Sandbox Code Playgroud)

感谢您阅读这个问题,希望我清楚我的问题是什么

Pra*_*lan 21

Date#toDateString方法将导致始终以该特定格式返回。

因此,要么您需要使用其他可用方法生成,要么您可以使用多种方法删除,


1. 使用String#split,Array#sliceArray#join

var mydate = new Date('29 Feb 2020');
// split  based on whitespace, then get except the first element
// and then join again
alert(mydate.toDateString().split(' ').slice(1).join(' '));
Run Code Online (Sandbox Code Playgroud)


2. 使用 String#replace

var mydate = new Date('29 Feb 2020');
// replace first nonspace combination along with whitespace
alert(mydate.toDateString().replace(/^\S+\s/,''));
Run Code Online (Sandbox Code Playgroud)


3. 使用String#indexOfString#substr
var mydate = new Date('29 Feb 2020');
// get index of first whitespace
var str = mydate.toDateString();
// get substring
alert(str.substr(str.indexOf(' ') + 1));
Run Code Online (Sandbox Code Playgroud)