我需要显示给定日期(如"2014年5月23日")的日期名称,这是我从第三方获得的.
我试过用Date
,但我只知道日期.
What is the correct way to get the name of the day?
我需要显示给定日期(如"2014年5月23日")的日期名称,这是我从第三方获得的.
我试过用Date
,但我只知道日期.
What is the correct way to get the name of the day?
You could use the Date.getDay()
method, which returns 0 for sunday, up to 6 for saturday. So, you could simply create an array with the name for the day names:
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var d = new Date(dateString);
var dayName = days[d.getDay()];
Here dateString
is the string you received from the third party API.
或者,如果您想要日期名称的前3个字母,可以使用Date
对象的内置toString
方法:
var d = new Date(dateString);
var dayName = d.toString().split(' ')[0];
这将采用d.toString()
输出中的第一个单词,即由3个字母组成的日期名称.