微信小程序—显示当前时间
生活随笔
收集整理的這篇文章主要介紹了
微信小程序—显示当前时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題:
在頁面上顯示當前時間(日期)
方法:
1、在util.js (創建項目自動生成)中:
1 // util.js 2 const formatTime = date => { 3 const year = date.getFullYear() 4 const month = date.getMonth() + 1 5 const day = date.getDate() 6 const hour = date.getHours() 7 const minute = date.getMinutes() 8 const second = date.getSeconds() 9 10 return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute].map(formatNumber).join(':') //返回年月日,時分秒 11 } 12 13 const formatNumber = n => { 14 n = n.toString() 15 return n[1] ? n : '0' + n 16 } 17 18 module.exports = { 19 formatTime: formatTime 20 }2、index.wxml中寫一個text顯示時間
1 <text class="user-motto">{{time}}</text>
3、index.js中寫邏輯
1 // index.js 2 3 var util = require('../../utils/util.js'); 4 Page({ 5 ...................... 6 /** 7 * 生命周期函數--監聽頁面加載 8 */ 9 onLoad: function (options) { 10 //日期顯示 11 var time = util.formatTime(new Date()) 12 //為頁面中time賦值 13 this.setData({ 14 time: time 15 }) 16 }, 17 ............................ 18 })4、重點:require 官方鏈接:https://developers.weixin.qq.com/minigame/dev/reference/api/require.html
any require(string path)
引入模塊。返回模塊通過
module.exports或exports暴露的接口。
名稱 類型 說明 path string 需要引入模塊文件相對于當前文件的相對路徑,或npm模塊名,或npm模塊路徑。不支持絕對路徑
總結
以上是生活随笔為你收集整理的微信小程序—显示当前时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 点云上的深度学习及其在三维场景理解中的应
- 下一篇: CAD