Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use JavaScript to figure out what day it is?

Shulou Source: shulou.com Published: 2022-06-03 07:32:17 10月02日 Update

This article mainly explains how to use JavaScript to figure out what day it is. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use JavaScript to figure out what day it is.

Methods: 1, define the array of "array" ("day", "one", "two", "three", "four", "five", "six"); 2, use the getDay () method of the Date object to obtain the number representing the current week; 3, take the number as the subscript and take the corresponding week value in the array.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

JavaScript calculates the current day of the week

Get current week / * get current day of the week * * / function getWeekDate () {var weeks = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); var now = new Date (); var day = now.getDay (); var week = weeks [day]; return week } alert (getWeekDate ())

Description:

The getDay () method returns a number that represents the day of the week.

DateObject.getDay ()

DateObject refers to the day of the week, using local time. The return value is an integer between 0 (Sunday) and 6 (Saturday).

So if you need to give the exact day of the week, you need to define an array of Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday").

Then take the number returned by getDay () as the subscript and fetch the corresponding week value from the array.

At this point, I believe you have a deeper understanding of "how to use JavaScript to figure out what day it is today". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Week number array method subscript content learning practical deeper between interest practicality actual object reference simple operation tutorial integer time more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi Apple MySQL Docker