JS帮你计算属相
/** * 该方法只能正确判定到2099年 * @auther 黑MAO * @time 2014年7月19日 */ ;(function(window){ /** * 生肖构造函数,默认参数是当前日期 * @param {Number} year 年 * @param {Number} month 月 * @param {Number} day 日 */ function Zodiac(year, month, day) { var date = new Date(); this.year = year*1 || date.getFullYear(); this.month = month*1 || date.getMonth(); this.day = day*1 || date.getDate(); } Zodiac.constructor = Zodiac; /** * 获取C值 * @return {Number} C */ Zodiac.prototype.getC = function(){ var _year = Math.floor(this.year/100)+1; var C; switch(_year){ case 20: C = 4.6295; break; case 21: C = 3.87; break; case 22: C = 4.15; break; default: C = 3.87; } return C; } /** * 获取立春日 一般都在2月 * @return {Number} springDay */ Zodiac.prototype.getSpringDay = function(){ var Y = this.year%100, D = 0.2422, C = this.getC(), L = (Y-1)/4, springDay = 0; springDay = Math.floor(Y*D+C)-Math.floor((Y-1)/4); return springDay; } /** * 获取生肖 * @return {String} myZodiac */ Zodiac.prototype.getZodiac = function(){ var year = this.year, month = this.month, day = this.day, zodiac = [‘子鼠‘,‘丑牛‘,‘寅虎‘,‘卯兔‘,‘辰龙‘,‘巳蛇‘,‘午马‘,‘未羊‘,‘申猴‘,‘酉鸡‘,‘戌狗‘,‘亥猪‘], myPos = (year-1900)%12, myZodiac = zodiac[myPos], springDay = this.getSpringDay(); switch(month){ case 1: var _myPos = myPos-1; if(_myPos<0){ _myPos = 11; } myZodiac = zodiac[_myPos]; break; case 2: if(day < springDay){ var _myPos = myPos-1; if(_myPos<0){ _myPos = 11; } myZodiac = zodiac[_myPos]; } break; } return myZodiac; } window.Zodiac = Zodiac; })(window);
//默认参数是当前日期 var zodiac = new Zodiac(); //var zodiac = new Zodiac(1980, 2, 3); //获取立春日 console.log(zodiac.getSpringDay()); //获取生肖 console.log(zodiac.getZodiac()); //可以与万年历进行比对 for(var year = 1900; year < 2100; year++) { var zodiac = new Zodiac(year, 2, 4); console.log(year, zodiac.getSpringDay(),zodiac.getZodiac()); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。