小程序使用时间转换

首先,在pages外,创建一个utils文件夹模块

在其中添加一个utils.js文件,添加代码如下

const formatTime =date=>{
  const year= date.getFullYear()
  const month=date.getMonth()+1
  const day=date.getDate()
  const hour=date.getHours()
  const minute=date.getMinutes()
  const second = date.getSeconds()

  return [year,month,day].map(formatNumber).join('/')+' '+[hour,minute,second].map(formatNumber).join(':')
}

const formatNumber = n =>{
  n=n.toString()
  return n[1]?n : '0'+n
}

function sayHello(name) {
  console.log(`Hello ${name} !`)
}

module.exports = {
  formatTime:formatTime,
  sayHello:sayHello
}

则,formatTime和sayHello可以作为函数供其它页面调用使用。

以index.js为例

在文件开头加上引用utils.js声明后


var utils= require('../../utils/utils.js')

就可以在函数体中加入utils具体使用了

console.log(utils.sayHello('index.js 测试 sayHello 函数'))

        var time = utils.formatTime(new Date())
        console.log(time)
        time = time.split("/", 2)[1]
        console.log(time)

以上的time可以作为变量在其它地方使用。

其他资料:

记录微信小程序显示当前年月日及其详细时间的代码 - 教程笔记 - 李洋个人博客

微信小程序日期转换、比较、加减 - 走看看

微信小程序--多种类型日期选择器(年月、月日...)_yoga今天敲代码了吗的博客-CSDN博客_微信小程序日期选择器

微信小程序获取当前日期和时间,并显示星期几_你觉得这样嘿怕吗的博客-CSDN博客_微信小程序显示日期

小程序的目录结构(微信)_seven豆的博客-CSDN博客_小程序目录结构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值