index.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * FeHelper Timestamp Tools
  3. */
  4. Tarp.require('../static/js/utils');
  5. new Vue({
  6. el: '#pageContainer',
  7. data: {
  8. txtNow: Math.round((new Date()).getTime() / 1000),
  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.txtNow = Math.round(nowDate.getTime() / 1000) + ' s / ' + nowDate.getTime() + ' ms';
  31. this.worldTime['local'] = this.txtNowDate;
  32. this.worldTime['gmt'] = gmtTime.format(formatter);
  33. for (let offset = -12; offset <= 12; offset++) {
  34. this.worldTime[ offset > 0 ? ('+' + offset) : offset] = new Date(gmtTime.getTime() + offset * 60 * 60000).format(formatter);
  35. }
  36. }, 1000);
  37. },
  38. unixToggle: function () {
  39. window.toggleModel = window.toggleModel || 0;
  40. if (window.toggleModel) {
  41. this.$refs.btnToggle.value = '暂停';
  42. window.toggleModel = 0;
  43. this.startTimestamp();
  44. } else {
  45. this.$refs.btnToggle.value = '开始';
  46. window.toggleModel = 1;
  47. window.clearInterval(window.intervalId);
  48. }
  49. },
  50. stampToLocale: function () {
  51. if (this.txtSrcStamp.length === 0) {
  52. alert('请先填写你需要转换的Unix时间戳');
  53. return;
  54. }
  55. if (!parseInt(this.txtSrcStamp, 10)) {
  56. alert('请输入合法的Unix时间戳');
  57. return;
  58. }
  59. let base = this.secFrom === 's' ? 1000 : 1;
  60. let format = 'yyyy-MM-dd HH:mm:ss' + (this.secFrom === 's' ? '' : ':SSS');
  61. this.txtDesDate = (new Date(parseInt(this.txtSrcStamp, 10) * base + ( (new Date()).getTimezoneOffset() + this.curGMT * 60 ) * 60000)).format(format);
  62. },
  63. localeToStamp: function () {
  64. if (this.txtLocale && !/\s\d+:\d+:\d+/.test(this.txtLocale)) {
  65. this.txtLocale += ' 00:00:00';
  66. }
  67. let locale = (new Date(Date.parse(this.txtLocale) - ( (new Date()).getTimezoneOffset() + this.curGMT * 60 ) * 60000)).getTime();
  68. if (isNaN(locale)) {
  69. alert('请输入合法的时间格式,如:2014-04-01 10:01:01,或:2014-01-01');
  70. }
  71. let base = this.secTo === 's' ? 1000 : 1;
  72. this.txtDesStamp = Math.round(locale / base);
  73. }
  74. }
  75. });