Home >Web Front-end >Front-end Q&A >javascript determine date day of week

javascript determine date day of week

王林
王林Original
2023-05-10 09:19:067468browse

When writing web applications, you may encounter situations where you need to handle dates and times. In JavaScript, we can use Date objects to represent dates and times and perform some related operations. In this article, we will introduce how to use JavaScript to determine the day of the week a date is.

1. Get the current date

In JavaScript, we can get the current date through the following statement:

var currentDate = new Date();

This will create a Date object, which represents the current date and time. We can use this object to obtain the year, month, number of days, hours, minutes, seconds and other information of the current date, for example:

var year = currentDate.getFullYear(); //获取当前年份
var month = currentDate.getMonth(); //获取当前月份(0-11)
var date = currentDate.getDate(); //获取当前天数(1-31)
var day = currentDate.getDay(); //获取当前星期几(0-6,0代表星期日,1代表星期一,依此类推)
var hour = currentDate.getHours(); //获取当前小时数(0-23)
var minute = currentDate.getMinutes(); //获取当前分钟数(0-59)
var second = currentDate.getSeconds(); //获取当前秒数(0-59)
var millisecond = currentDate.getMilliseconds(); //获取当前毫秒数

2. Convert the date string to a Date object

In In actual development, we sometimes need to convert strings into Date objects in order to perform operations such as date calculations. We can use the following code to convert the date string to a Date object:

var dateString = "2022-12-12"; //定义日期字符串
var dateObject = new Date(dateString); //将字符串转换为Date对象

Note that the date string must conform to the ISO 8601 format, which is yyyy-mm-dd (such as "2022-12-12") or yyyy /mm/dd (e.g. "2022/12/12"). If the string format does not match, Invalid Date will be returned.

3. Determine the day of the week the date is

In JavaScript, we can use the getDay() method of the Date object to get the day of the week the current date is. Specifically, the method returns a value that represents the day of the week in the range 0-6, where 0 represents Sunday, 1 represents Monday, and so on. We can combine the switch statement or if statement to determine the day of the week. For example:

var date = new Date("2022-12-12"); //定义日期对象
var day = date.getDay(); //获取星期几
switch (day) {
    case 0:
        console.log("日期是星期日");
        break;
    case 1:
        console.log("日期是星期一");
        break;
    case 2:
        console.log("日期是星期二");
        break;
    case 3:
        console.log("日期是星期三");
        break;
    case 4:
        console.log("日期是星期四");
        break;
    case 5:
        console.log("日期是星期五");
        break;
    case 6:
        console.log("日期是星期六");
        break;
}

Or use if statement:

var date = new Date("2022-12-12"); //定义日期对象
var day = date.getDay(); //获取星期几
if (day === 0) {
    console.log("日期是星期日");
} else if (day === 1) {
    console.log("日期是星期一");
} else if (day === 2) {
    console.log("日期是星期二");
} else if (day === 3) {
    console.log("日期是星期三");
} else if (day === 4) {
    console.log("日期是星期四");
} else if (day === 5) {
    console.log("日期是星期五");
} else if (day === 6) {
    console.log("日期是星期六");
}

4. Determine the number of days between two dates

Sometimes, we need to calculate the number of days between two dates difference. You can use the following code:

var Date1 = new Date('2018-08-08');
var Date2 = new Date('2018-08-12');
var diffDays = parseInt(Math.abs(Date2 - Date1) / (1000 * 60 * 60 * 24));
console.log(diffDays);

In the code, we first instantiate two date objects, namely Date1 and Date2. We then calculated the difference in days between them by dividing their difference by the number of milliseconds per day. Finally, we use the parseInt() method to extract the integer part from the result to get the full number of days difference. In other words, in the above example, there is a difference of 4 days between Date1 and Date2.

5. Conclusion

In JavaScript, we can use Date objects to represent dates and times and perform some related operations. We can use the getDay() method to get the day of the week, or we can use other methods to get the year, month, number of days, hours, minutes, seconds and other information. Allows us to handle dates and times more flexibly to meet the needs of application development.

The above is the detailed content of javascript determine date day of week. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn