Guide to Using the getDay method of the Date object in javascript, dategetday
The Date object has a getDay method, which returns the day of a week in a specific Date based on the local time. The return value ranges from 0 ~ 6, corresponding to Sunday ~ Saturday
GetDay 0 1 2 3 4 5 6
Day of the week, Sunday, Tuesday, Thursday, Friday, Saturday
When using date-related requirements, you need to convert the value returned by getDay to the day of the week, that is, what is the day of the week? For example, if you select a calendar in the calendar component, "Monday" is returned ".
This is a piece of code that is still running online.
Copy codeThe Code is 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 'wedned ';
} Else if (numWeekDay = 4 ){
Return 'thursday ';
} Else if (numWeekDay = 5 ){
Return 'Friday ';
} Else if (numWeekDay = 6 ){
Return 'saturday ';
} Else {
Return '';
}
}
This code is determined by multiple if else branches and returns the day of the week. Some people mentioned that the switch can be used for optimization.
Copy codeThe Code is 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 'wedned ';
Case 4: return 'thursday ';
Case 5: return 'Friday ';
Case 6: return 'saturday ';
Default: return '';
}
}
Compared with if/else, the code is simpler and clearer. The shorter the Statistical Code that someone has done, the shorter the time for the brain to think. Therefore, you will see various "the beauty of short codes", "the simple way of code", and other people and books that advocate and praise "short codes.
Code Daquan mentions the use of table-driven methods to simplify programming
The table-driven method is a programming mode (scheme) that searches for information from the table without using logical statements (if and switch ). In fact, you can select a logical statement from a table. In simple cases, it is easier and straightforward to use logical statements. However, as the logic chain becomes more complex, the lookup method becomes more attractive.
As mentioned above, using tables instead of logical statements, many front-end engineers in JS have tried their best to use expressions to eliminate statements since learning some features of functional languages. For example
1. & replace a single if
Copy codeThe Code is as follows:
If (a = 1 ){
$. Ajax (xx)
}
// -->
(A = 1) & $. ajax (xx)
2 .? : Replace if/else
Copy codeThe Code is as follows:
If (a = 1 ){
$. Ajax (xx)
} Else {
$ (Yy). remove ()
}
// -->
(A = 1 )? $. Ajax (xx): $ (yy). remove ()
3. Multiple if/else and switch can also use multiple "? : "Replace
Copy codeThe Code is 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 use the function recursion to eliminate the for/while statement. I began to become addicted to these writing methods, and later found that I could not understand them (maybe I still read less, the brain always naturally converts these idioms), and finally I used to use statements.
We use a JS object to replace the table mentioned in code Daquan.
Copy codeThe Code is 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': 'weday ',
'4': 'thursday ',
'5': 'Friday ',
'6': 'satur ',
};
Return weekObj [numWeekDay] | '';
}
Compared with switch, it reduces a lot of code, but there is still 0 ~ The key with 6 Characters exists. The returned value of the getDay method starts from 0 exactly the same as that of the JS array index. Therefore, the array can be simplified.
Copy codeThe Code is 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', 'tues', 'wedned', 'thurs', 'Friday', 'satur'];
Return weekArr [numWeekDay] | '';
}