twelve.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /***************************************************************************
  2. * 计算生肖
  3. *
  4. * 支持简写生日,比如01,转换为2001,89转换为1989; 支持任何可以进行时间转换的格式,比如'1989/01/01','1989 01'等
  5. *
  6. */
  7. export function getShengXiao(birth) {
  8. birth += '';
  9. var len = birth.length;
  10. if (len < 4 && len != 2) {
  11. return "";
  12. }
  13. if (len == 2) {
  14. birth - 0 > 30 ? birth = '19' + birth : birth = '20' + birth;
  15. }
  16. var year = (new Date(birth)).getFullYear();
  17. var arr = ['猴', '鸡', '狗', '猪', '鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊'];
  18. return /^\d{4}$/.test(year) ? arr[year % 12] : "";
  19. }
  20. /***************************************************************************
  21. * 计算星座
  22. *
  23. *
  24. */
  25. export function getAstro(y, m, d) {
  26. var constellations = [{
  27. "Start": 121,
  28. "End": 220,
  29. "Name": "水平座"
  30. }, {
  31. "Start": 221,
  32. "End": 320,
  33. "Name": "双鱼座"
  34. }, {
  35. "Start": 321,
  36. "End": 420,
  37. "Name": "白羊座"
  38. }, {
  39. "Start": 421,
  40. "End": 520,
  41. "Name": "金牛座"
  42. }, {
  43. "Start": 521,
  44. "End": 620,
  45. "Name": "双子座"
  46. }, {
  47. "Start": 621,
  48. "End": 720,
  49. "Name": "巨蟹座"
  50. }, {
  51. "Start": 721,
  52. "End": 820,
  53. "Name": "狮子座"
  54. }, {
  55. "Start": 821,
  56. "End": 920,
  57. "Name": "处女座"
  58. }, {
  59. "Start": 921,
  60. "End": 1020,
  61. "Name": "天秤座"
  62. }, {
  63. "Start": 1021,
  64. "End": 1120,
  65. "Name": "天蝎座"
  66. }, {
  67. "Start": 1121,
  68. "End": 1220,
  69. "Name": "射手座"
  70. }];
  71. /*
  72. * 判断日期有效性 1,3,5,7,8,10,12为31天 2月润年29,非润年28 4,6,9,11为30天
  73. */
  74. var daysInMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  75. // 检测年份
  76. if (y < 1970 || y > 2099)
  77. return "";
  78. // 检测月份
  79. if (m < 1 || m > 12)
  80. return "";
  81. // 检测日期
  82. var mDays = daysInMonth[m - 1];
  83. // 如果是二月,要根据年份计算天数,不是二月时略过此计算
  84. if (m == 2) {
  85. mDays = GetSpecialDays(y)
  86. }
  87. // 判断日数据是不是在月份的有效天范围
  88. if (d < 0 || d > mDays)
  89. return "";
  90. // 好了,走到这一步,说明上面的验证都TM过了。
  91. // 这才判断是哪一个星座
  92. // 星座座标等于m*100 + d
  93. var pos = parseInt(m * 100) + parseInt(d);
  94. for (var i in constellations) {
  95. if (pos >= constellations[i].Start && pos <= constellations[i].End) {
  96. return constellations[i].Name;
  97. }
  98. }
  99. }
  100. // 根据年份计算二月天数
  101. export function GetSpecialDays(y) {
  102. if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
  103. return 29;
  104. return 28;
  105. }
  106. /***************************************************************************
  107. * 计算年龄
  108. *
  109. * 支持传递["yyyy-MM-dd"]等格式的字符串
  110. *
  111. */
  112. export function getAge(str) {
  113. var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
  114. if (r == null)
  115. return false;
  116. var d = new Date(r[1], r[3] - 1, r[4]);
  117. if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] &&
  118. d.getDate() == r[4]) {
  119. var Y = new Date().getFullYear();
  120. return (Y - r[1]);
  121. }
  122. return "";
  123. }
  124. /*
  125. * 当type=1时获取出生日期,type=2时获取性别,type=3时获取年龄
  126. * */
  127. export function IdCard(IdCard, type) {
  128. if (type === 1) {
  129. //获取出生日期
  130. let birthday = IdCard.substring(6, 10) + "-" + IdCard.substring(10, 12) + "-" + IdCard.substring(12, 14)
  131. return birthday
  132. }
  133. if (type === 2) {
  134. //获取性别
  135. if (parseInt(IdCard.substr(16, 1)) % 2 === 1) {
  136. return "男"
  137. } else {
  138. return "女"
  139. }
  140. }
  141. if (type === 3) {
  142. //获取年龄
  143. var ageDate = new Date()
  144. var month = ageDate.getMonth() + 1
  145. var day = ageDate.getDate()
  146. var age = ageDate.getFullYear() - IdCard.substring(6, 10) - 1
  147. if (IdCard.substring(10, 12) < month || IdCard.substring(10, 12) === month && IdCard.substring(12, 14) <= day) {
  148. age++
  149. }
  150. if (age <= 0) {
  151. age = 1
  152. }
  153. return age
  154. }
  155. }