使用Javascript日期显示星期几

时间:2013-10-25 07:09:03

标签: javascript

我使用下面的代码显示将来7天的日期。但是,这个javascript代码将日期格式化为mm / dd / yyyy。

我想修改下面的javascript代码,让它显示星期几,例如星期五,11月1日

我不需要它来显示年份。

任何帮助都会非常感激,因为我很难过。

<script>
var myDate=new Date();
myDate.setDate(myDate.getDate()+7);
var n=myDate.toLocaleDateString();
document.write(n);
</script>

2 个答案:

答案 0 :(得分:4)

Date有一个getDay()方法,它返回一个日期编号,从星期日0开始,继续星期一= 1,星期二= 2,依此类推。

您可以使用它来返回工作日。请参阅How to get the day from a particular date using JavaScript和类似问题。

document.write(["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][myDate.getDay()]);

答案 1 :(得分:3)

试试这个,从当天输出的1周后你得到的是: 11月1日星期五

<script >
<!--

var m_names = ["January", "February", "March", 
"April", "May", "June", "July", "August", "September", 
"October", "November", "December"];

var d_names = ["Sunday","Monday", "Tuesday", "Wednesday", 
"Thursday", "Friday", "Saturday"];

var myDate = new Date();
myDate.setDate(myDate.getDate()+7);
var curr_date = myDate.getDate();
var curr_month = myDate.getMonth();
var curr_day  = myDate.getDay();
document.write(d_names[curr_day] + "," + m_names[curr_month] + " " +curr_date);

//-->
</script>