Home >Web Front-end >Front-end Q&A >javascript format date day of the week
JavaScript is a powerful programming language that can be used for a variety of purposes. Among them, processing date and time is also one of its common applications. In this article, we will take a closer look at how to use JavaScript to format a date and get the day of the week.
Date object in JavaScript
In JavaScript, you can use the Date object to represent date and time. Creating a Date object is very simple, just use the new operator. For example:
var today = new Date();
This will create a Date object of the current date and time. You can use the Date object to obtain the year, month, day, hour, minute, second and other information, for example:
var year = today.getFullYear(); var month = today.getMonth(); var day = today.getDate(); var hours = today.getHours(); var minutes = today.getMinutes(); var seconds = today.getSeconds();
This will obtain the year, month, day, hour, minute and second of the current date and time respectively.
Formatting Dates and Times
While Date objects make it easy to obtain individual parts of dates and times, formatting dates and times in a specific format requires the use of other techniques. In JavaScript, you can use string replacement and regular expressions to accomplish this task. Let's look at an example:
var date = new Date(); var formattedDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
In this example, we first create a Date object and use the getFullYear, getMonth and getDate methods to get the year, month and day. We then concatenate them and prepend the month with 1 since getMonth returns a number from 0 to 11. In this way, you get a date string in the form of "2022-10-24", which is one of the common date formats.
Get the day of the week
Getting the day of the week is one of the common tasks in JavaScript. To achieve this we need to use the getDay method. getDay returns a number from 0 to 6, representing Sunday to Saturday. So, if we want to get the current day of the week, we can use the following code:
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; var today = new Date(); var dayOfWeek = days[today.getDay()];
In this example, we first create an array to store the strings corresponding to Sunday to Saturday. Then, we create a Date object and use the getDay method to get the day of the week represented by this object, and then use an array to get the corresponding week string.
If you want to display the date and day of the week in a specific format, you can combine the above methods. For example:
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; var date = new Date(); var formattedDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate(); var dayOfWeek = days[date.getDay()]; var formattedDateTime = formattedDate + ' (' + dayOfWeek + ')';
In this example, we first obtain the date string and weekday string according to the above method, and then connect them with brackets to get something like "2022-10-24 (Monday)" Formatted date and day of the week strings.
Summary
In JavaScript, you can easily get the various parts of a date and time using the Date object. To format dates and times, you can use string replacement and regular expressions. To get the day of the week, you can use the getDay method and combine it with an array of strings. Combining these methods makes it easy to get formatted datetime and day of the week.
The above is the detailed content of javascript format date day of the week. For more information, please follow other related articles on the PHP Chinese website!