index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <el-container>
  3. <el-header>
  4. <h1>XD</h1>
  5. <el-divider></el-divider>
  6. </el-header>
  7. <el-main>
  8. <div class="hidden-xs-only">
  9. <dater-larger :date="date" :anotherDate="anotherDate" :options="options" @calculate="calculate"/>
  10. </div>
  11. <div class="hidden-sm-and-up">
  12. <dater-smaller :date="date" :anotherDate="anotherDate" :options="options" @calculate="calculate"/>
  13. </div>
  14. </el-main>
  15. <el-footer>
  16. <center style="color:#C0C4CC;">
  17. &copy;&nbsp;zbLiuLiu&nbsp;{{year}}&nbsp;-&nbsp;本项目完全开源免费,基于Vue.js、Nuxt.js、Element UI制作。
  18. </center>
  19. </el-footer>
  20. </el-container>
  21. </template>
  22. <script>
  23. import Dater1 from '../components/DaterLarger.vue'
  24. import DaterSmaller from '../components/DaterSmaller.vue'
  25. export default {
  26. components: { Dater1 },
  27. data(){
  28. return {
  29. date: {
  30. year:0,
  31. month:0,
  32. day:0,
  33. hour:0,
  34. minute:0,
  35. second:0
  36. },
  37. anotherDate: {
  38. year:0,
  39. month:0,
  40. day:0,
  41. hour:0,
  42. minute:0,
  43. second:0
  44. },
  45. options:[
  46. {label:"加",value:"add", script:function(a,b) {
  47. return a+b
  48. }},
  49. {label:"减",value:"subtract", script:function(a,b) {
  50. return a-b
  51. }},
  52. ],
  53. option:"",
  54. year: (new Date()).getFullYear()
  55. }
  56. },
  57. methods:{
  58. calculate(d,a,o,component){
  59. var func = function(a){return a};
  60. for (const index in this.options) {
  61. if (Object.hasOwnProperty.call(this.options, index)) {
  62. const option = this.options[index];
  63. if(option.value==o){
  64. func = option.script;
  65. break;
  66. }
  67. }
  68. }
  69. var date = this.dater(d);
  70. var anotherDate = this.dater(a);
  71. // console.log(date,anotherDate)
  72. var year = func(date.getFullYear(),anotherDate.getFullYear()),
  73. month = func(date.getMonth(),anotherDate.getMonth()),
  74. day = func(date.getDate()-1,anotherDate.getDate()-1),
  75. hour = func(date.getHours(),anotherDate.getHours()),
  76. minute = func(date.getMinutes(),anotherDate.getMinutes()),
  77. second = func(date.getSeconds(),anotherDate.getSeconds());
  78. var thirdDate = new Date();
  79. thirdDate.setTime(0);
  80. thirdDate.setFullYear(year);
  81. thirdDate.setMonth(month);
  82. thirdDate.setDate(day+1);
  83. thirdDate.setHours(hour);
  84. thirdDate.setMinutes(minute);
  85. thirdDate.setSeconds(second);
  86. // console.log(thirdDate)
  87. this.date.year = thirdDate.getFullYear()
  88. this.date.month = thirdDate.getMonth()
  89. this.date.day = thirdDate.getDate()-1
  90. this.date.hour = thirdDate.getHours()
  91. this.date.minute = thirdDate.getMinutes()
  92. this.date.second = thirdDate.getSeconds()
  93. component.focus('anotherDateYear')
  94. },
  95. dater(date){
  96. var year = date.year||0,
  97. month = date.month||0,
  98. day = date.day+1||0,
  99. hour = date.hour||0,
  100. minute = date.minute||0,
  101. second = date.second||0;
  102. var dateObj = new Date();
  103. dateObj.setTime(0);
  104. dateObj.setFullYear(year);
  105. dateObj.setMonth(month);
  106. dateObj.setDate(day);
  107. dateObj.setHours(hour);
  108. dateObj.setMinutes(minute);
  109. dateObj.setSeconds(second);
  110. return dateObj;
  111. }
  112. },
  113. mounted(){
  114. setInterval(()=>{
  115. this.year = (new Date()).getFullYear()
  116. },100)
  117. }
  118. }
  119. </script>