且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript从日期中删除日期名称

更新时间:2023-01-31 16:09:53

The Date#toDateString method would result always returns in that particular format.

因此,您需要使用其他可用的方法来生成,或者可以使用几种方法来删除,

So either you need to generate using other methods available or you can remove using several ways,


1.使用 String#split 代码> Array#slice Array#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(' '));


2.使用 String#replace 代码>

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


3.使用 String#indexOf 代码> String#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));