You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

59 lines
2.5 KiB

  1. /**
  2. * @description 根据时间戳获取时间
  3. * @param {*} timestamp 必传,number类型,时间戳数据(10位及以下,10位至13位);若不传,则返回:“无时间戳”
  4. * @param {*} format 选传,string类型,提供以下时间格式:YYYY-MM-DD hh:mm:ss、YYYY/MM/DD hh:mm:ss、YYYY.MM.DD hh:mm:ss、YYYY MM DD hh:mm:ss、YYYY年MM月DD日 hh:mm:ss、YYYY-MM-DD、YYYY/MM/DD、YYYY.MM.DD、YYYY MM DD、YYYY年MM月DD日;若不传,则默认为:YYYY-MM-DD
  5. * @returns 根据要求的时间格式
  6. * @version V 1.0, Created by YWQ, 2022.10.20
  7. */
  8. export const timestampToTime = (timestamp, format) => {
  9. //时间戳为10位需*1000,时间戳为13位不需乘1000
  10. const length = timestamp.length
  11. if (length <= 10) {
  12. var date = new Date(timestamp * 1000)
  13. } else {
  14. var date = new Date(timestamp)
  15. }
  16. let Y = String(date.getFullYear())
  17. let M = String(date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
  18. let D = String(date.getDate() + 1 < 10 ? '0' + (date.getDate()) : date.getDate())
  19. let h = String(date.getHours() + 1 < 10 ? '0' + (date.getHours()) : date.getHours())
  20. let m = String(date.getMinutes() + 1 < 10 ? '0' + (date.getMinutes()) : date.getMinutes())
  21. let s = String(date.getSeconds() + 1 < 10 ? '0' + (date.getSeconds()) : date.getSeconds())
  22. // return Y + M + D + h + m + s
  23. if (format == "YYYY-MM-DD hh:mm:ss") {
  24. return Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s
  25. } else if (format == "YYYY/MM/DD hh:mm:ss") {
  26. return Y + "/" + M + "/" + D + " " + h + ":" + m + ":" + s
  27. } else if (format == "YYYY.MM.DD hh:mm:ss") {
  28. return Y + "." + M + "." + D + " " + h + ":" + m + ":" + s
  29. } else if (format == "YYYY MM DD hh:mm:ss") {
  30. return Y + " " + M + " " + D + " " + h + ":" + m + ":" + s
  31. } else if (format == "YYYY年MM月DD日 hh:mm:ss") {
  32. return Y + "年" + M + "月" + D + "日" + " " + h + ":" + m + ":" + s
  33. } else if (format == "YYYY-MM-DD") {
  34. return Y + "-" + M + "-" + D
  35. } else if (format == "YYYY/MM/DD") {
  36. return Y + "/" + M + "/" + D
  37. } else if (format == "YYYY.MM.DD") {
  38. return Y + "." + M + "." + D
  39. } else if (format == "YYYY MM DD") {
  40. return Y + " " + M + " " + D
  41. } else if (format == "YYYY年MM月DD日") {
  42. return Y + "年" + M + "月" + D + "日"
  43. } else {
  44. return Y + "-" + M + "-" + D
  45. }
  46. }
  47. /**
  48. * @description:滚动到对应id的位置
  49. * @param {type} id
  50. */
  51. export const scrollToID = id => {
  52. wx.createSelectorQuery().select("#" + id).scrollIntoView({
  53. behavior: "smooth",
  54. block: "start",
  55. inline: "nearest",
  56. });
  57. }