返回

前端面试必备:三道常见的面试题轻松搞定!

前端

前端面试必备:三道常见的面试题轻松搞定!

前端开发面试中,对于日期的处理常常是必考题。本文将介绍三个常见的面试题,帮助你轻松应对前端开发面试。

1. 如何用 JavaScript 获取当前日期的星期几?

// 获取当前日期的星期几
const day = new Date().getDay();

// 将数字转换为星期几的字符串
const weekDays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
const weekDay = weekDays[day];

// 输出星期几
console.log(weekDay);

2. 如何用 Vue.js 实现时间限制,今天之前不可选?

<template>
  <div>
    <input type="date" v-model="selectedDate">
    <button @click="submitForm">提交</button>
  </div>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const selectedDate = ref('');

    const submitForm = () => {
      // 检查选定的日期是否在今天之前
      const today = new Date();
      const selectedDateObj = new Date(selectedDate.value);
      if (selectedDateObj > today) {
        alert('选定的日期不能在今天之后');
        return;
      }

      // 表单提交
      // ...
    };

    return {
      selectedDate,
      submitForm,
    };
  },
};
</script>

3. 如何用 JavaScript 计算某个时间的前 15 天和后 15 天?

// 计算某个时间的前 15 天和后 15 天
const date = new Date();

// 计算前 15 天
const previousDate = new Date(date.getTime() - 15 * 24 * 60 * 60 * 1000);

// 计算后 15 天
const nextDate = new Date(date.getTime() + 15 * 24 * 60 * 60 * 1000);

// 输出结果
console.log(`前 15 天:${previousDate}`);
console.log(`后 15 天:${nextDate}`);

结语

以上三个面试题是前端开发面试中常见的日期处理问题。希望通过本文的介绍,能够帮助你轻松应对前端开发面试。