JS中日期的日期名称 [英] Day Name from Date in JS

查看:151
本文介绍了JS中日期的日期名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示一个日期的名称(如05/23/2014),这是我从第三方获得的。

I need to display the name of the day given a date (like "05/23/2014") which I get from a 3rd party.

我'我尝试使用日期,但我只得到了日期。

I've tried using Date, but I only get the date.

获取当天名称的正确方法是什么?

What is the correct way to get the name of the day?

推荐答案

您可以使用 Date.getDay()方法,该方法在星期日返回0,在星期六返回6。因此,您只需创建一个包含日期名称名称的数组:

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()];

这里 dateString 是你收到的字符串第三方API。

Here dateString is the string you received from the third party API.

或者,如果你想要当天名字的前3个字母,你可以使用日期对象的内置 toString 方法:

Alternatively, if you want the first 3 letters of the day name, you could use the Date object's built-in toString method:

var d = new Date(dateString);
var dayName = d.toString().split(' ')[0];

这将占用 d.toString()中的第一个字输出,这将是3个字母的日期名称。

That will take the first word in the d.toString() output, which will be the 3-letter day name.

这篇关于JS中日期的日期名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆