Getday Methods for date objects in JavaScript using the guide _javascript tips

Source: Internet
Author: User
Tags local time

The Date object has a Getday method that returns the day ordinal of a week in a specific date, based on the local time. Return values from 0~6, respectively, Sunday ~ Saturday

Getday 0 1 2 3 4 5 6
Weeks Sunday Monday Tuesday Wednesday Thursday Friday Saturday

When you use date-related requirements, you need to turn the value returned by Getday to the day of the week. For example, select a calendar in the Calendar component and return to "2014-12-22 Monday."

This is a piece of code that still runs on the line.

Copy Code code as follows:

/*
* Returns the day of the week based on the Date object
* @param {Date} date
* @return {String} "Wednesday"
*/
function Getchineseweekbydate (date) {
var numweekday = Date.getday ();
if (Numweekday = = 0) {
Return ' Sunday ';
else if (Numweekday = 1) {
Return ' Monday ';
else if (Numweekday = 2) {
Return ' Tuesday ';
else if (Numweekday = 3) {
Return ' Wednesday ';
else if (Numweekday = 4) {
Return ' Thursday ';
else if (Numweekday = 5) {
Return ' Friday ';
else if (Numweekday = 6) {
Return ' Saturday ';
} else {
Return ";
}
}

This code is judged by multiple if else branches and returns the day of the week, with some students referring to the use of the switch to optimize

Copy Code code as follows:

/*
* Returns the day of the week based on the Date object
* @param {Date} date
* @return {String} "Wednesday"
*/
function Getchineseweekbydate (date) {
var numweekday = Date.getday ();
Switch (numweekday) {
Case 0:return ' Sunday ';
Case 1:return ' Monday ';
Case 2:return ' Tuesday ';
Case 3:return ' Wednesday ';
Case 4:return ' Thursday ';
Case 5:return ' Friday ';
Case 6:return ' Saturday ';
Default:return ';
}
}

Compared with If/else, the code is simpler and shorter and clearer. The shorter the statistical code is, the less time the brain has to think. So you will see a variety of "short Code of the United States", "Code Concise Way" and other advocates and praise "short Code" people and books.

Code Encyclopedia mentions the use of table-driven methods to simplify programming

Table-driven-table-driven method is a programming pattern (scheme) that looks for information from the list without using logical statements (if and switch). In fact, all the choices that can be chosen through logical statements can be found by looking up tables. In simple cases, it is easier and more straightforward to use logical statements. But as the logic chain becomes more complex, the look-up table approach becomes more appealing.

The above mentioned that the use of tables to replace logical statements, JS has many front-end engineers since the understanding of functional language features, desperately use expression to eliminate the statement. Like what

1. && Alternative Single If

Copy Code code as follows:

if (a = = 1) {
$.ajax (XX)
}
-->
(A = = 1) && $.ajax (XX)

2.?: Alternative If/else

Copy Code code as follows:

if (a = = 1) {
$.ajax (XX)
} else {
$ (yy). Remove ()
}
-->
(A = = 1)? $.ajax (XX): $ (yy). Remove ()

3. Multiple if/else and switch can also be replaced with multiple "?:"

Copy Code code as follows:

if (a = = 1) {
Alert (1)
else if (a = = 2) {
Alert (2)
else if (a = = 3) {
Alert (3)
} else {
Alert (4)
}
-->
(A = = 1)
? Alert (1): (A = = 2)
? Alert (2): (A = = 3)
? Alert (3): Alert (4)

In addition, you can also use function to return to eliminate For/while statements. The beginning of these writing more addictive, later found that they do not understand (may still see less, the brain is always natural and convert these into statements), and finally the habit of using statements.

"Code Encyclopedia," said the table, we use a JS object to replace the test

Copy Code code as follows:

/*
* Returns the day of the week based on the Date object
* @param {Date} date
* @return {String} "Wednesday"
*/
function Getchineseweekbydate (date) {
var numweekday = Date.getday ();
var weekobj = {
' 0 ': ' Sunday ',
' 1 ': ' Monday ',
' 2 ': ' Tuesday ',
' 3 ': ' Wednesday ',
' 4 ': ' Thursday ',
' 5 ': ' Friday ',
' 6 ': ' Saturday ',
};
return Weekobj[numweekday] | | '';
}

Compared to switch, but also reduced a lot of code, but there are 0~6 the word key exists. The Getday method returns starting at 0 just like the JS array index from 0. So the array can be simplified

Copy Code code as follows:

/*
* Returns the day of the week based on the Date object
* @param {Date} date
* @return {String} "Wednesday"
*/
function Getchineseweekbydate (date) {
var numweekday = Date.getday ();
var Weekarr = [' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday '];
return Weekarr[numweekday] | | '';
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.