FormatTime.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. function changeTime(cellValue) {
  2. var arr = cellValue.split('T')
  3. var d = arr[0]
  4. var darr = d.split('-')
  5. var t = arr[1]
  6. var tarr = t.split('.000')
  7. var marr = tarr[0].split(':')
  8. var dd = parseInt(darr[0]) + '/' + parseInt(darr[1]) + '/' + parseInt(darr[2]) + ' ' + parseInt(marr[0]) + ':' + parseInt(marr[1]) + ':' + parseInt(marr[2])
  9. return this.formatDateTime(dd)
  10. }
  11. function formatDateTime(date) {
  12. var time = new Date(Date.parse(date))
  13. time.setTime(time.setHours(time.getHours() + 8))
  14. var Y = time.getFullYear() + '-'
  15. var M = this.addZero(time.getMonth() + 1) + '-'
  16. var D = this.addZero(time.getDate()) + ' '
  17. var h = this.addZero(time.getHours()) + ':'
  18. var m = this.addZero(time.getMinutes()) + ':'
  19. var s = this.addZero(time.getSeconds())
  20. return Y + M + D + h + m + s
  21. }
  22. /**
  23. * 通过时间戳获取当前时间
  24. * @returns
  25. */
  26. function getNowDate(timestamp) {
  27. var nowDate = new Date(timestamp);
  28. // var year = nowDate.getFullYear(); //获取当前年
  29. // var mon = nowDate.getMonth() + 1; //获取当前月
  30. // var date = nowDate.getDate(); //获取当前日
  31. // var hours = nowDate.getHours(); //获取当前小时
  32. // var minutes = nowDate.getMinutes(); //获取当前分钟
  33. // var seconds = nowDate.getSeconds(); //获取当前秒
  34. // var now = year + "-" + mon + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
  35. // return now;
  36. return CstTimeToNomalTime(nowDate)
  37. }
  38. function addZero(num) {
  39. return num < 10 ? '0' + num : num
  40. }
  41. function CstTimeToNomalTime(time) {
  42. return `${time.getFullYear()}-${time.getMonth() + 1 >= 10 ? (time.getMonth() + 1) : '0' + (time.getMonth() + 1)}-${time.getDate() >= 10 ? time.getDate() : '0' + time.getDate()} ${time.getHours() >= 10 ? time.getHours() : '0' + time.getHours()}:${time.getMinutes() >= 10 ? time.getMinutes() : '0' + time.getMinutes()}:${time.getSeconds() >= 10 ? time.getSeconds() : '0' + time.getSeconds()}`;
  43. }