Javascript从日期中删除日期名称 [英] Javascript Remove Day Name from Date
本文介绍了Javascript从日期中删除日期名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想问一下是否有人从以下示例中删除日期名称",该警报返回2020年2月29日星期六,我不仅使用Moment.js jQuery,因为我只需要能够处理日期即可下面写为代码的格式.
i wanted to ask if someone knows how to remove the Day Name from the following example,the alert returns Sat Feb 29 2020, im not using Moment.js only Jquery because i only need to be able to handle the date in the format that is written below as code.
var mydate = new Date('29 Feb 2020');
alert(mydate.toDateString());
感谢您阅读此问题,希望我弄清楚我的问题是什么
Thank you for reading this question and hope i make clear what my problem is
推荐答案
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));
这篇关于Javascript从日期中删除日期名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文