dateTime.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. Component({
  2. properties: {
  3. //1-日期+时间(年月日+时分) 2-日期(年月日) 3-日期(年月) 4-时间(时分)
  4. type: {
  5. type: Number,
  6. value: 1,
  7. observer(val) {
  8. this.propsChange();
  9. }
  10. },
  11. //年份区间
  12. startYear: {
  13. type: Number,
  14. value: 1980,
  15. observer(val) {
  16. this.propsChange();
  17. }
  18. },
  19. //年份区间
  20. endYear: {
  21. type: Number,
  22. value: 2050,
  23. observer(val) {
  24. this.propsChange();
  25. }
  26. },
  27. //"取消"字体颜色
  28. cancelColor: {
  29. type: String,
  30. value: "#888"
  31. },
  32. //"确定"字体颜色
  33. color: {
  34. type: String,
  35. value: "#5677fc"
  36. },
  37. //设置默认显示日期 2019-08-01 || 2019-08-01 17:01 || 2019/08/01
  38. setDateTime: {
  39. type: String,
  40. value: "",
  41. observer(val) {
  42. this.propsChange();
  43. }
  44. }
  45. },
  46. lifetimes: {
  47. ready: function () {
  48. this.initData()
  49. }
  50. },
  51. data: {
  52. isShow: false,
  53. years: [],
  54. months: [],
  55. days: [],
  56. hours: [],
  57. minutes: [],
  58. year: 0,
  59. month: 0,
  60. day: 0,
  61. hour: 0,
  62. minute: 0,
  63. startDate: "",
  64. endDate: "",
  65. value: [0, 0, 0, 0, 0],
  66. reset: false
  67. },
  68. observers:{
  69. 'year, month': function (year, month) {
  70. this.setDays();
  71. }
  72. },
  73. methods: {
  74. stop() {},
  75. propsChange() {
  76. this.setData({
  77. reset: true
  78. })
  79. setTimeout(() => {
  80. this.initData()
  81. }, 10);
  82. },
  83. formatNum: function(num) {
  84. return num < 10 ? "0" + num : num + "";
  85. },
  86. generateArray: function(start, end) {
  87. return Array.from(new Array(end + 1).keys()).slice(start)
  88. },
  89. getIndex: function(arr, val) {
  90. let index = arr.indexOf(val);
  91. return ~index ? index : 0
  92. },
  93. //日期时间处理
  94. initSelectValue() {
  95. let fdate = this.data.setDateTime.replace(/\-/g, '/');
  96. fdate = fdate && fdate.indexOf("/") == -1 ? `2020/01/01 ${fdate}` : fdate
  97. let time = null;
  98. if (fdate)
  99. time = new Date(fdate);
  100. else
  101. time = new Date();
  102. this.setData({
  103. year: time.getFullYear(),
  104. month: time.getMonth() + 1,
  105. day: time.getDate(),
  106. hour: time.getHours(),
  107. minute: time.getMinutes()
  108. })
  109. },
  110. initData() {
  111. this.initSelectValue()
  112. this.setData({
  113. reset: false
  114. })
  115. switch (this.data.type) {
  116. case 1:
  117. this.setData({
  118. value: [0, 0, 0, 0, 0]
  119. })
  120. this.setYears();
  121. this.setMonths();
  122. this.setDays();
  123. this.setHours();
  124. this.setMinutes();
  125. break;
  126. case 2:
  127. this.setData({
  128. value: [0, 0, 0]
  129. })
  130. this.setYears();
  131. this.setMonths();
  132. this.setDays();
  133. break;
  134. case 3:
  135. this.setData({
  136. value: [0, 0]
  137. })
  138. this.setYears();
  139. this.setMonths();
  140. break;
  141. case 4:
  142. this.setData({
  143. value: [0, 0]
  144. })
  145. this.setHours();
  146. this.setMinutes();
  147. break;
  148. default:
  149. break;
  150. }
  151. },
  152. setYears() {
  153. this.setData({
  154. years: this.generateArray(this.data.startYear, this.data.endYear)
  155. })
  156. setTimeout(() => {
  157. let value = "value[0]";
  158. this.setData({
  159. [value]: this.getIndex(this.data.years, this.data.year)
  160. })
  161. }, 8);
  162. },
  163. setMonths() {
  164. this.setData({
  165. months: this.generateArray(1, 12)
  166. })
  167. setTimeout(() => {
  168. let value = "value[1]";
  169. this.setData({
  170. [value]: this.getIndex(this.data.months, this.data.month)
  171. })
  172. }, 8);
  173. },
  174. setDays() {
  175. if (this.data.type == 3 || this.data.type == 4) return;
  176. let totalDays = new Date(this.data.year, this.data.month, 0).getDate();
  177. this.setData({
  178. days: this.generateArray(1, totalDays)
  179. })
  180. setTimeout(() => {
  181. let value = "value[2]";
  182. this.setData({
  183. [value]: this.getIndex(this.data.days, this.data.day)
  184. })
  185. }, 8);
  186. },
  187. setHours() {
  188. this.setData({
  189. hours: this.generateArray(0, 23)
  190. })
  191. setTimeout(() => {
  192. let value = `value[${this.data.value.length - 2}]`;
  193. this.setData({
  194. [value]: this.getIndex(this.data.hours, this.data.hour)
  195. })
  196. }, 8);
  197. },
  198. setMinutes() {
  199. this.setData({
  200. minutes: this.generateArray(0, 59)
  201. })
  202. setTimeout(() => {
  203. let value = `value[${this.data.value.length - 1}]`;
  204. this.setData({
  205. [value]: this.getIndex(this.data.minutes, this.data.minute)
  206. })
  207. }, 8);
  208. },
  209. show() {
  210. setTimeout(() => {
  211. this.setData({
  212. isShow: true
  213. })
  214. }, 50);
  215. },
  216. hide() {
  217. this.setData({
  218. isShow: false
  219. })
  220. },
  221. change(e) {
  222. this.setData({
  223. value: e.detail.value
  224. })
  225. switch (this.data.type) {
  226. case 1:
  227. this.setData({
  228. year: this.data.years[this.data.value[0]],
  229. month: this.data.months[this.data.value[1]],
  230. day: this.data.days[this.data.value[2]],
  231. hour: this.data.hours[this.data.value[3]],
  232. minute: this.data.minutes[this.data.value[4]]
  233. })
  234. break;
  235. case 2:
  236. this.setData({
  237. year: this.data.years[this.data.value[0]],
  238. month: this.data.months[this.data.value[1]],
  239. day: this.data.days[this.data.value[2]]
  240. })
  241. break;
  242. case 3:
  243. this.setData({
  244. year: this.data.years[this.data.value[0]],
  245. month: this.data.months[this.data.value[1]]
  246. })
  247. break;
  248. case 4:
  249. this.setData({
  250. hour: this.data.hours[this.data.value[0]],
  251. minute: this.data.minutes[this.data.value[0]]
  252. })
  253. break;
  254. default:
  255. break;
  256. }
  257. },
  258. btnFix() {
  259. let result = {};
  260. let year = this.data.year;
  261. let month = this.formatNum(this.data.month || 0);
  262. let day = this.formatNum(this.data.day || 0);
  263. let hour = this.formatNum(this.data.hour || 0);
  264. let minute = this.formatNum(this.data.minute || 0);
  265. switch (this.data.type) {
  266. case 1:
  267. result = {
  268. year: year,
  269. month: month,
  270. day: day,
  271. hour: hour,
  272. minute: minute,
  273. result: `${year}-${month}-${day} ${hour}:${minute}`
  274. }
  275. break;
  276. case 2:
  277. result = {
  278. year: year,
  279. month: month,
  280. day: day,
  281. result: `${year}-${month}-${day}`
  282. }
  283. break;
  284. case 3:
  285. result = {
  286. year: year,
  287. month: month,
  288. result: `${year}-${month}`
  289. }
  290. break;
  291. case 4:
  292. result = {
  293. hour: hour,
  294. minute: minute,
  295. result: `${hour}:${minute}`
  296. }
  297. break;
  298. default:
  299. break;
  300. }
  301. this.triggerEvent('confirm', result)
  302. this.hide();
  303. }
  304. }
  305. })