1、使用if语句
var str = ""; var week = new Date().getDay(); if (week == 0) { str = "今天是星期日"; } else if (week == 1) { str = "今天是星期一"; } else if (week == 2) { str = "今天是星期二"; } else if (week == 3) { str = "今天是星期三"; } else if (week == 4) { str = "今天是星期四"; } else if (week == 5) { str = "今天是星期五"; } else if (week == 6) { str = "今天是星期六"; }
2、使用switch case语句
var str1 = "今天是星期"; var week = new Date().getDay(); switch (week) { case 0 : str1 += "日"; break; case 1 : str1 += "一"; break; case 2 : str1 += "二"; break; case 3 : str1 += "三"; break; case 4 : str1 += "四"; break; case 5 : str1 += "五"; break; case 6 : str1 += "六"; break; } alert(str1);
3、神器,一句话解决
var str = "今天是星期" + "日一二三四五六".charAt(new Date().getDay()); alert(str);
时间: 2024-11-03 05:41:15