js通过年月日(生日或者日期)获取年龄和生肖和星座的示例代码
分类:Javascript| 发布:佚名| 查看:1551 | 发表时间:2014/9/12
02 | function parseDate(str){ |
03 | if (str.match(/^\d{4}[\-\/\s+]\d{1,2}[\-\/\s+]\d{1,2}$/)){ |
04 | return new Date(str.replace(/[\-\/\s+]/i, '/' )) |
05 | } else if (str.match(/^\d{8}$/)){ |
06 | return new Date(str.substring(0,4)+ '/' +str.substring(4,6)+ '/' +str.substring(6)) |
08 | alert( 'date parse error' ) |
15 | var thisYear=aDate.getFullYear(); |
16 | var thisMonth=aDate.getMonth()+1; |
17 | var thisDay=aDate.getDate(); |
18 | var brith=parseDate(document.getElementById( "date1" ).value); |
19 | brithy=brith.getFullYear(); |
20 | brithm=brith.getMonth()+1; |
21 | brithd=brith.getDate(); |
22 | if (thisYear-brithy<0) { |
26 | if (thisMonth-brithm<0){ |
27 | age = thisYear-brithy-1; |
29 | if (thisDay-brithd>=0) { |
31 | age = thisYear-brithy; |
33 | age = thisYear-brithy-1; |
37 | document.getElementById( "date2" ).value=age; |
HTML代码
2 | 生日:< input name = "date1" id = "date1" onblur = "getAge()" > |
3 | 年龄:< input name = "date2" id = "date2" > |
-----------------------------------------------华丽的分割线-------
02 | function getshengxiao(yyyy){ |
03 | var arr=[ '猴' , '鸡' , '狗' , '猪' , '鼠' , '牛' , '虎' , '兔' , '龙' , '蛇' , '马' , '羊' ]; |
04 | return /^\d{4}$/.test(yyyy)?arr[yyyy%12]: null |
08 | function getAstro(month,day){ |
09 | var s= "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯" ; |
10 | var arr=[20,19,21,21,21,22,23,23,23,23,22,22]; |
11 | return s.substr(month*2-(day<arr[month-1]?2:0),2); |
结合AngularJs模板使用例子:
1 | < tr ng-repeat = "fans in vm.data.users" > |
2 | < td ng-bind = "$index+1" ></ td > |
3 | < td ng-bind = "fans.nickname" >郑州网建</ td > |
4 | < td ng-bind = "fans.gender=='m'?'男':'女'" ></ td > |
5 | < td ng-bind = "vm.getDate(fans.birthday)" ></ td > |
6 | < td ng-bind = "vm.getAstro(fans.birthday)+'座'" ></ td > |
7 | < td ng-bind = "vm.unix_to_datetime(1399259663*1000)" ></ td > |
PS:unix时间戳转换成日期时间:(关于上边时间戳1399259663为什么*1000,是因为根据不同的时区的问题,见下边的相关教程链接。)此转换后的结果是:2014/5/5 11:14:23
02 | unix_to_datetime: function (datetime) { |
04 | datetime = datetime.replace( ' +0800' , '' ); |
06 | var now = new Date(datetime); |
07 | var year = now.getFullYear(), |
08 | month = now.getMonth()+1, |
10 | hour = now.getHours(), |
11 | minut = now.getMinutes(), |
12 | secon = now.getSeconds(); |
23 | return year+ "/" +month+ "/" +day+ " " +hour+ ":" +minut+ ":" +secon; |
相关教程: UTC和CST和GMT时间的区别