Skip to content

Instantly share code, notes, and snippets.

@xuanfeng
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuanfeng/76cf2193211a3d771894 to your computer and use it in GitHub Desktop.
Save xuanfeng/76cf2193211a3d771894 to your computer and use it in GitHub Desktop.
JS按时间区间显示不同信息
// 按不同的时间段显示不同的“问好”信息,一般做法是
// if(hour>=0 && hour <=5;){
// }else if(hour>=6 && hour <=8;){
// }else if(hour>=9 && hour <=11;){}
// ...
// 这里换了一种做法,看起来相对简洁一点!
/*
Test case:
for(var i = 0,iMax = 23; i <=iMax;i++){
console.log(i, timeHello(i));
}
*/
function timeHello(h){
var t = [6,9,12,14,18,24];
for(var i in t){
if(h < t[i]){
return ['凌晨好!','早上好!','上午好!','中午好!','下午好!','晚上好!'][i]
}
};
}
/*
timeHello(new Date().getHours())
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment