我需要显示给定日期(如"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个字母组成的日期名称.

Jquery相关问答推荐

使用动态类名的gm_addStyle?

Django BaseDataTableView-Filter_queryset方法不起作用

使用带有 bootstrap 验证的 Ajax 函数时出现问题

如何从 onclick div 属性检测 Javascript 中的 control+click?

jQuery函数从数组中获取所有唯一元素?

寻找 jQuery 事件类型的完整列表

如何使用 jQuery 禁用粘贴(Ctrl+V)?

在 jQuery 中将 JSON 数组转换为 HTML 表

jQuery用另一个类替换一个类

删除索引后的所有项目

jQuery中的Grep与过滤器?

好处和.在本地托管 jQuery 的trap

Twitter Bootstrap css 类的 jQuery show() 隐藏

使用 AJAX 加载 Bootstrap 弹出内容.这可能吗?

如何隐藏 Twitter Bootstrap 下拉菜单

如何使用 jQuery 取消设置元素的 CSS 属性?

jQuery - 添加 ID 而不是类

如何判断值是否为 JSON 对象?

在 JavaScript 开头使用分号的目的是什么?

Bootstrap:在 Modal 中打开另一个 Modal