当前位置:   article > 正文

微信小程序调用时间的三种方法_微信小程序开发如何获取当前时间并调用

微信小程序开发如何获取当前时间并调用

1.更改后端时间戳

  1. //wxml
  2. <wxs module="Formater" src="../../../utils/Datetime.wxs"></wxs>
  3. <text>{{Formater.formatDatetime(publisherTime,"yyyy/MM/dd hh:mm")}}</text>
  4. //ts
  5. data:{
  6. publisherTime: 1660714987000,
  7. }
  1. //日期格式化----Datatime代码段---------------------------------------------------------
  2. var formatDatetime = function (timestamp, format = "yyyy-MM-dd hh:mm:ss") {
  3. if (!timestamp) {
  4. return "null"
  5. }
  6. var realDate = getDate(timestamp);
  7. var regYear = getRegExp("(y+)", "i");
  8. var date = [
  9. ["M+", realDate.getMonth() + 1],
  10. ["d+", realDate.getDate()],
  11. ["h+", realDate.getHours()],
  12. ["m+", realDate.getMinutes()],
  13. ["s+", realDate.getSeconds()],
  14. ["q+", Math.floor((realDate.getMonth() + 3) / 3)],
  15. ["S+", realDate.getMilliseconds()],
  16. ];
  17. var reg1 = regYear.exec(format);
  18. if (reg1) {
  19. format = format.replace(reg1[1], (realDate.getFullYear() + '').substring(4 - reg1[1].length));
  20. }
  21. for (var i = 0; i < date.length; i++) {
  22. var reg2 = getRegExp("(" + date[i][0] + ")").exec(format);
  23. if (reg2) {
  24. format = format.replace(reg2[1], reg2[1].length == 1 ? v : ("00" + date[i][1]).substring(("" + date[i][1]).length));
  25. }
  26. }
  27. return format;
  28. };
  29. var timeUnitFormat = function (unit) {
  30. var map = {
  31. "HOURS": "时",
  32. "DAY": "天",
  33. "MONTH": "月",
  34. "YEAR": "年"
  35. }
  36. return map[unit] ? map[unit] : "null";
  37. }
  38. var inclued = function (array, item) {
  39. return array.indexOf(item) > -1
  40. }
  41. var formatNum = function (n, m = 0) {
  42. return n.toFixed(m)
  43. }
  44. module.exports = {
  45. formatDatetime: formatDatetime, //日期格式化
  46. timeUnitFormat: timeUnitFormat,
  47. publisherTypeFormat: publisherTypeFormat,
  48. formatNum: formatNum,
  49. inclued: inclued
  50. }

2.更改后端时间戳

  1. formatTimeTwo(number, format) {
  2. var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  3. var returnArr = [];
  4. var date = new Date(number * 1000);
  5. returnArr.push(date.getFullYear());
  6. returnArr.push((date.getMonth() + 1));
  7. returnArr.push((date.getDate()));
  8. returnArr.push((date.getHours()));
  9. returnArr.push((date.getMinutes()));
  10. returnArr.push((date.getSeconds()));
  11. for (var i in returnArr) {
  12. format = format.replace(formateArr[i], returnArr[i]);
  13. }
  14. return format;
  15. },

3.获取本地时间

  1. let dataTime
  2. let yy = new Date().getFullYear()
  3. let mm = new Date().getMonth()+1
  4. let dd = new Date().getDate()
  5. let hh = new Date().getHours()
  6. let mf = new Date().getMinutes()<10?'0'+new Date().getMinutes():
  7. new Date().getMinutes()
  8. let ss = new Date().getSeconds()<10?'0'+new Date().getSeconds():
  9. new Date().getSeconds()
  10. dataTime = `${yy}.${mm}.${dd} ${hh}:${mf}:${ss}`;
  11. this.setData({
  12. dataTime:dataTime
  13. })
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/651030
推荐阅读
相关标签
  

闽ICP备14008679号