index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * FeHelper Timestamp Tools
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. txtNowS: Math.round((new Date()).getTime() / 1000),
  8. txtNowMs: (new Date()).getTime(),
  9. txtNowDate: (new Date()).toLocaleString(),
  10. txtSrcStamp: '',
  11. txtDesDate: '',
  12. txtLocale: '',
  13. txtDesStamp: '',
  14. secFrom: 's',
  15. secTo: 's',
  16. worldTime: {},
  17. curGMT: (new Date()).getTimezoneOffset() / 60 * -1
  18. },
  19. mounted: function () {
  20. this.startTimestamp();
  21. },
  22. methods: {
  23. startTimestamp: function () {
  24. let formatter = 'yyyy-MM-dd HH:mm:ss';
  25. window.intervalId = window.setInterval(() => {
  26. let localDate = new Date();
  27. let gmtTime = new Date(localDate.getTime() + localDate.getTimezoneOffset() * 60000);
  28. let nowDate = new Date(gmtTime.getTime() + this.curGMT * 60 * 60000);
  29. this.txtNowDate = nowDate.format(formatter);
  30. this.txtNowS = Math.round(nowDate.getTime() / 1000);
  31. this.txtNowMs = nowDate.getTime();
  32. this.worldTime['local'] = this.txtNowDate;
  33. this.worldTime['gmt'] = gmtTime.format(formatter);
  34. for (let offset = -12; offset <= 12; offset++) {
  35. this.worldTime[offset > 0 ? ('+' + offset) : offset] = new Date(gmtTime.getTime() + offset * 60 * 60000).format(formatter);
  36. }
  37. }, 1000);
  38. },
  39. unixToggle: function () {
  40. window.toggleModel = window.toggleModel || 0;
  41. if (window.toggleModel) {
  42. this.$refs.btnToggle.value = '暂停';
  43. window.toggleModel = 0;
  44. this.startTimestamp();
  45. } else {
  46. this.$refs.btnToggle.value = '开始';
  47. window.toggleModel = 1;
  48. window.clearInterval(window.intervalId);
  49. }
  50. },
  51. stampToLocale: function () {
  52. if (this.txtSrcStamp.length === 0) {
  53. alert('请先填写你需要转换的Unix时间戳');
  54. return;
  55. }
  56. if (!parseInt(this.txtSrcStamp, 10)) {
  57. alert('请输入合法的Unix时间戳');
  58. return;
  59. }
  60. let base = this.secFrom === 's' ? 1000 : 1;
  61. let format = 'yyyy-MM-dd HH:mm:ss' + (this.secFrom === 's' ? '' : '.SSS');
  62. this.txtDesDate = (new Date(parseInt(this.txtSrcStamp, 10) * base + ((new Date()).getTimezoneOffset() + this.curGMT * 60) * 60000)).format(format);
  63. },
  64. localeToStamp: function () {
  65. if (this.txtLocale && !/\s\d+:\d+:\d+/.test(this.txtLocale)) {
  66. this.txtLocale += ' 00:00:00';
  67. }
  68. let locale = (new Date(Date.parse(this.txtLocale) - ((new Date()).getTimezoneOffset() + this.curGMT * 60) * 60000)).getTime();
  69. if (isNaN(locale)) {
  70. alert('请输入合法的时间格式,如:2014-04-01 10:01:01,或:2014-01-01');
  71. }
  72. let base = this.secTo === 's' ? 1000 : 1;
  73. this.txtDesStamp = Math.round(locale / base);
  74. },
  75. copyToClipboard(text) {
  76. if (!text || !(text || '').trim().length) return;
  77. let input = document.createElement('textarea');
  78. input.style.position = 'fixed';
  79. input.style.opacity = 0;
  80. input.value = text;
  81. document.body.appendChild(input);
  82. input.select();
  83. document.execCommand('Copy');
  84. document.body.removeChild(input);
  85. this.toast('已复制到剪贴板,随处粘贴可用:[ ' + text + ' ]');
  86. },
  87. toast(content) {
  88. window.clearTimeout(window.feHelperAlertMsgTid);
  89. let elAlertMsg = document.querySelector("#fehelper_alertmsg");
  90. if (!elAlertMsg) {
  91. let elWrapper = document.createElement('div');
  92. elWrapper.innerHTML = '<div id="fehelper_alertmsg">' + content + '</div>';
  93. elAlertMsg = elWrapper.childNodes[0];
  94. document.body.appendChild(elAlertMsg);
  95. } else {
  96. elAlertMsg.innerHTML = content;
  97. elAlertMsg.style.display = 'block';
  98. }
  99. window.feHelperAlertMsgTid = window.setTimeout(function () {
  100. elAlertMsg.style.display = 'none';
  101. }, 3000);
  102. }
  103. }
  104. });