Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

221 строка
5.4 KiB

  1. <!-- -->
  2. <template>
  3. <div class="about">
  4. <div class="mt10">时区时间转化:</div>
  5. <el-input placeholder="请输入内容"
  6. v-model="time"
  7. class="input-with-select mt10">
  8. <el-select v-model="select"
  9. slot="prepend"
  10. placeholder="请选择">
  11. <el-option :label="item.label"
  12. :value="item.diff"
  13. v-for="item of utcList"
  14. :key="item.lable"></el-option>
  15. </el-select>
  16. <el-button slot="append"
  17. icon="el-icon-search"
  18. @click="showList">转化</el-button>
  19. </el-input>
  20. <div class="mt10">时间戳为:{{ currTime }} {{ nowStr }}</div>
  21. <div class="mt10">所有时区时间戳为:</div>
  22. <div>
  23. <span v-for="(item,index) of unixList"
  24. :key="index"
  25. class="time-item">{{ item.label }}:{{ item.unix }}</span>
  26. </div>
  27. <el-divider></el-divider>
  28. <div class="mt10">时间格式化:</div>
  29. <div v-for="item of list"
  30. :key="item"
  31. class="mt10">{{ item|friendlyTimeFilter }}</div>
  32. <div class="mt10">
  33. <el-button class=""
  34. type="primary"
  35. icon="el-icon-view"
  36. size="mini"
  37. @click="downloadClick">查看代码</el-button>
  38. </div>
  39. </div>
  40. </template>
  41. <script>
  42. //这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
  43. //例如:import 《组件名称》 from '《组件路径》';
  44. import dayjs from "dayjs";
  45. export default {
  46. name: "About",
  47. props: {},
  48. //import引入的组件需要注入到对象中才能使用
  49. components: {
  50. //defaultPage ,
  51. },
  52. data () {
  53. return {
  54. list: [],
  55. select: 0,
  56. utcList: [],
  57. time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  58. unixList: [],
  59. currTime: "",
  60. nowStr:"",
  61. }
  62. },
  63. //监听属性 类似于data概念
  64. computed: {
  65. key () {
  66. return this.$route.fullPath
  67. }
  68. },
  69. //监控data中的数据变化
  70. watch: {},
  71. //生命周期 - 创建完成(可以访问当前this实例)
  72. async created () {
  73. },
  74. //生命周期 - 挂载完成(可以访问DOM元素)
  75. async mounted () {
  76. this.initList();
  77. this.test();
  78. },
  79. //方法集合
  80. methods: {
  81. test () {
  82. for (let i = -11; i <= 12; i++) {
  83. let label = i;
  84. if (i == 0) {
  85. label = ""
  86. } else if (i > 0) {
  87. label = `+${i}`
  88. }
  89. this.utcList.push({
  90. diff: i,
  91. label: `UTC${label}`
  92. })
  93. }
  94. let str1 = "2023-08-21T23:59:59+08:00"
  95. let str2 = "2023-08-21T23:59:59-05:00"
  96. let utc = "2023-08-21T23:59:59+00:00"
  97. console.log("东八区时间:", str1, dayjs(str1).unix());
  98. console.log("西五区时间:", str2, dayjs(str2).unix());
  99. console.log("utc时间:", utc, dayjs(utc).unix());
  100. this.showList();
  101. },
  102. showList () {
  103. let list = []
  104. for (let item of this.utcList) {
  105. let utcStr = "";
  106. if (Math.abs(item.diff) < 10) {
  107. utcStr = `0${Math.abs(item.diff)}:00`;
  108. } else {
  109. utcStr = `${Math.abs(item.diff)}:00`;
  110. }
  111. utcStr = `${item.diff >= 0 ? '+' : '-'}${utcStr}`;
  112. let timeStr = this.time.replace(" ", "T") + utcStr;
  113. let timeObj = dayjs(timeStr);
  114. list.push({
  115. label: item.label,
  116. unix: timeObj.unix(),
  117. })
  118. if (item.diff == this.select) {
  119. this.currTime = timeObj.unix();
  120. this.nowStr = timeStr;
  121. }
  122. }
  123. this.unixList = list;
  124. },
  125. initList () {
  126. let list = [];
  127. let curr = dayjs();
  128. list.push(
  129. curr.subtract(10, "s").unix()
  130. )
  131. list.push(
  132. curr.subtract(10, "m").unix()
  133. )
  134. list.push(
  135. curr.subtract(62, "m").unix()
  136. )
  137. list.push(
  138. curr.subtract(10, "h").unix()
  139. )
  140. list.push(
  141. curr.subtract(10, "day").unix()
  142. )
  143. list.push(
  144. curr.subtract(1, "year").unix()
  145. )
  146. this.list = list;
  147. },
  148. downloadClick () {
  149. window.open("/static/code.txt")
  150. // window.location.href = "/code.txt"
  151. }
  152. },
  153. //当前页面的filter
  154. filters: {
  155. friendlyTimeFilter (val) {
  156. if (!val) {
  157. return "--";
  158. }
  159. let length = `${val}`.length;
  160. if (length != 13 && length != 10) {
  161. return "--";
  162. }
  163. if (length == 13) {
  164. val = parseInt(val / 1000);
  165. }
  166. let valObj = dayjs(val * 1000);
  167. let currObj = dayjs();
  168. if (!valObj.isSame(currObj, "year")) {
  169. return valObj.format("MMM DD,YYYY")
  170. }
  171. let curr = currObj.unix();
  172. let diff = curr - val;
  173. if (diff < 60) {
  174. return "a minute ago";
  175. } else if (diff < 60 * 60) {
  176. return `${parseInt(diff / 60)} minutes ago`;
  177. } else if (diff < 60 * 60 * 2) {
  178. return `a hour ago`;
  179. } else if (diff < 60 * 60 * 24) {
  180. return `${parseInt(diff / (60 * 60))} hours ago`;
  181. }
  182. return valObj.format("MMM DD")
  183. }
  184. }
  185. }
  186. </script>
  187. <!-- 单页私有css -->
  188. <style scoped>
  189. .about {
  190. padding: 30px;
  191. background-color: #fff;
  192. border-radius: 4px;
  193. min-height: 600px;
  194. }
  195. .mt10 {
  196. margin-top: 10px;
  197. }
  198. .about /deep/ .el-select .el-input {
  199. width: 130px;
  200. }
  201. .input-with-select {
  202. width: 500px;
  203. }
  204. .input-with-select /deep/ .el-input-group__prepend {
  205. background-color: #fff;
  206. }
  207. .time-item {
  208. display: inline-block;
  209. width: 300px;
  210. }
  211. </style>
  212. <style>
  213. </style>