[英]Javascript Remove Day Name from Date
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.我想问一下是否有人知道如何从以下示例中删除日期名称,警报返回 2020 年 2 月 29 日星期六,我不使用 Moment.js 仅 Jquery,因为我只需要能够以以下格式处理日期下面写成代码。
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. Date#toDateString
方法将导致始终以该特定格式返回。
So either you need to generate using other methods available or you can remove using several ways,因此,要么您需要使用其他可用方法生成,要么您可以使用多种方法删除,
String#split
, Array#slice
and Array#join
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(' '));
String#replace
String#replace
var mydate = new Date('29 Feb 2020'); // replace first nonspace combination along with whitespace alert(mydate.toDateString().replace(/^\\S+\\s/,''));
String#indexOf
and String#substr
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));
If you've got a Date object instance and only want some parts of it I'd go with the Date object API :如果您有一个 Date 对象实例并且只想要它的某些部分,我会使用Date 对象 API :
mydate.getDate() + ' ' + mydate.toLocaleString('en-us', { month: "short" }) + ' ' + mydate.getFullYear()
Just keep in mind the functions are local time based (there are UTC variants, eg getUTCDate()
), also to prevent some confusion getMonth()
is zero-based.请记住,函数是基于本地时间的(有 UTC 变体,例如
getUTCDate()
),同时为了防止混淆, getMonth()
是基于零的。 Working with dates in JavaScript is where the real fun begins ;)在 JavaScript 中处理日期才是真正有趣的开始;)
The toLocaleString
function is relatively new though (IE11+), check other possibilities if you need to support older browsers. toLocaleString
函数虽然相对较新(IE11+),如果您需要支持旧浏览器,请检查其他可能性。
Easiest way is to replace all alpha characters from a string.最简单的方法是替换字符串中的所有字母字符。 That way you will not make mistake once day name is in a different position.
这样,一旦天名处于不同的位置,您就不会出错。
var withoutDay = '29 Feb 2020'.replace(/[a-zA-Z]{0,1}/g,'').replace(' ', ' '); alert(withoutDay);
Code replace(/[a-zA-Z]{0,1}/g,'')
will replace all alpha characters from string and replace(' ', ' ');
代码
replace(/[a-zA-Z]{0,1}/g,'')
将替换字符串中的所有字母字符并replace(' ', ' ');
will remove double spaces.将删除双空格。
I hope this helps.我希望这有帮助。
Proper way is to use DateTimeFormat.正确的方法是使用 DateTimeFormat。 You can play around by manipulating the format object inside DateTimeFormat.
您可以通过操作 DateTimeFormat 中的格式对象来进行操作。
let myDate = new Date('29 Feb 2020'); let formattedDate = new Intl.DateTimeFormat("en-US", { year: "numeric", month: "short", day: "2-digit", }).format(myDate); alert(formattedDate)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.