一:请用JavaScript实现显示“今天星期几”。使用时间对象,结合数组,打印“今天是星期几”。
时间: 2024-03-24 13:39:40 浏览: 115
好的,下面是JavaScript代码实现:
```javascript
// 创建表示星期的数组
var weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
// 创建表示今天日期的时间对象
var today = new Date();
// 获取今天是星期几
var dayOfWeek = weekdays[today.getDay()];
// 打印今天是星期几
console.log('今天是' + dayOfWeek);
```
运行上面的代码,就可以在控制台中看到输出的结果,例如:今天是星期四。
相关问题
请用JavaScript实现显示“今天星期几”。(任选一种及以上方法) 方法1:使用时间对象,结合数组,打印“今天是星期几”。 方法2:使用时间对象,结合选择结构if,打印“今天是星期几”。 方法3:使用时间对象,结合选择结构switch,打印“今天是星期几”。
方法1:
```javascript
const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const today = new Date().getDay();
console.log(`Today is ${daysOfWeek[today]}.`);
```
方法2:
```javascript
const today = new Date().getDay();
let dayOfWeek;
if (today === 0) {
dayOfWeek = 'Sunday';
} else if (today === 1) {
dayOfWeek = 'Monday';
} else if (today === 2) {
dayOfWeek = 'Tuesday';
} else if (today === 3) {
dayOfWeek = 'Wednesday';
} else if (today === 4) {
dayOfWeek = 'Thursday';
} else if (today === 5) {
dayOfWeek = 'Friday';
} else if (today === 6) {
dayOfWeek = 'Saturday';
}
console.log(`Today is ${dayOfWeek}.`);
```
方法3:
```javascript
const today = new Date().getDay();
let dayOfWeek;
switch (today) {
case 0:
dayOfWeek = 'Sunday';
break;
case 1:
dayOfWeek = 'Monday';
break;
case 2:
dayOfWeek = 'Tuesday';
break;
case 3:
dayOfWeek = 'Wednesday';
break;
case 4:
dayOfWeek = 'Thursday';
break;
case 5:
dayOfWeek = 'Friday';
break;
case 6:
dayOfWeek = 'Saturday';
break;
}
console.log(`Today is ${dayOfWeek}.`);
```
一:请用JavaScript实现显示“今天星期几”。(任选一种及以上方法)方法1:使用时间对象,结合数组,打印“今天是星期几”。方法2:使用时间对象,结合选择结构if,打印“今天是星期几”。方法3:使用时间对象,结合选择结构switch,打印“今天是星期几”。二:请获取今年的年份,通过if选择结构来判断输入的年份是否为闰年。(使用时间对象,获取年份函数getFullYear(),结合选择结构if。闰年leap year,即能被4整除且不能整除100 或者能够被400整除的是闰年。)
好的,我来回答你的问题。
一、以下是三种不同的 JavaScript 实现方法:
方法1:使用时间对象,结合数组,打印“今天是星期几”。
```javascript
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var today = new Date();
var dayOfWeek = days[today.getDay()];
console.log("Today is " + dayOfWeek);
```
方法2:使用时间对象,结合选择结构if,打印“今天是星期几”。
```javascript
var today = new Date();
var dayOfWeek = today.getDay();
if (dayOfWeek === 0) {
console.log("Today is Sunday");
} else if (dayOfWeek === 1) {
console.log("Today is Monday");
} else if (dayOfWeek === 2) {
console.log("Today is Tuesday");
} else if (dayOfWeek === 3) {
console.log("Today is Wednesday");
} else if (dayOfWeek === 4) {
console.log("Today is Thursday");
} else if (dayOfWeek === 5) {
console.log("Today is Friday");
} else if (dayOfWeek === 6) {
console.log("Today is Saturday");
}
```
方法3:使用时间对象,结合选择结构switch,打印“今天是星期几”。
```javascript
var today = new Date();
var dayOfWeek = today.getDay();
switch (dayOfWeek) {
case 0:
console.log("Today is Sunday");
break;
case 1:
console.log("Today is Monday");
break;
case 2:
console.log("Today is Tuesday");
break;
case 3:
console.log("Today is Wednesday");
break;
case 4:
console.log("Today is Thursday");
break;
case 5:
console.log("Today is Friday");
break;
case 6:
console.log("Today is Saturday");
break;
}
```
二、获取今年的年份,通过if选择结构来判断输入的年份是否为闰年。
```javascript
var year = new Date().getFullYear();
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
console.log(year + " is a leap year.");
} else {
console.log(year + " is not a leap year.");
}
```
以上就是我对你的问题的回答,希望能够帮到你。如果还有其他问题,请随时提出。
阅读全文