更新时间:2022-04-10 04:58:31
如果您在在您的项目中,
If you include this function somewhere in your project,
// Accepts a Date object or date string that is recognized by the Date.parse() method
function getDayOfWeek(date) {
var dayOfWeek = new Date(date).getDay();
return isNaN(dayOfWeek) ? null : ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][dayOfWeek];
}
您将能够轻松地使用它:
You will be able to use it anywhere easily like this:
getDayOfWeek("2013-07-31")
> "Wednesday"
getDayOfWeek(new Date())
> // (will return today's day. See demo jsfiddle below...)
如果无效的日期字符串是使用,将返回null。
If invalid date string is used, a null will be returned.
getDayOfWeek("~invalid~");
> null
有效的日期字符串基于 Date.parse()方法,如MDN JavaScript参考中所述。
Valid date strings are based on the Date.parse() method as described in the MDN JavaScript reference.
演示: http:// jsfiddle。 net / samliew / fo1nnsgp /
当然你也可以使用 moment.js 插件,特别是如果涉及时区。
Of course you can also use the moment.js plugin, especially if timezones are involved.