微信小程序日期选择器显示当前系统年月日时分

小程序vant-weapp的日期选择器的使用(年月日时分)

话不多说,记录一下这个框架的使用~小程序使用轻量、可靠的小程序 UI 组件库 vant-weapp
Github源码:https://github.com/youzan/vant-weapp
中文文档:https://youzan.github.io/vant-weapp/#/intro


1:打开微信开发者工具,填写自己的appid和项目名称,选择不使用云服务,新建一个项目。

image
image

2:右击在选择在终端打开

image

进入项目的根目录底下,注意,一定要进入根目录哦,使用cd ../返回上一级目录~

image

3:小程序已经支持使用 npm 安装第三方包,
这里通过 npm 安装

   1、第一步:npm init

   2、第二步:npm install --production

   3、第三步: npm i @vant/weapp -S --production
      或者  npm i vant-weapp -S --production

image
image
image

这里需要注意一下
npm i vant-weapp -S --production或者npm i @vant/weapp -S --production
引入的区别

使用npm i vant-weapp安装的时候,到时候在在app.json或index.json中引入组件,需要使用这样的路径

{
  "usingComponents": {
    "van-button": "../../miniprogram_npm/vant-weapp/button/index"
  }
}

使用npm i @vant/weapp安装的时候,到时候在在app.json或index.json中引入组件,需要使用这样的路径(推荐,因为这个可以直接抄文档,不需要改变引入路径的~)

{
"usingComponents": {
  "van-button": "@vant/weapp/button/index"
}
}

4:在微信开发工具执行npm 构建,点击工具里面,构建npm

image

构建过程需要等待一会儿,不要捉急

image

构建完会生成一个miniprogram_npm文件夹
如果构建完如果编译报错,再构建一次就好了

image
话不多说,来看看小程序vant-weapp的日期选择器的使用

日期选择器文档参照一下

https://youzan.github.io/vant-weapp/#/datetime-picker

5:使用DatetimePicker 时间选择组件

选择日期格式如下:

2021-06-25 9:29

参考代码:
test.wxml

当前选择:{{currentChoose}}


  

test.js


const app = getApp()
Page({
  data: {
    minHour: 0,
    maxHour: 24,
    minDate: new Date(1990,1,1).getTime(),
    maxDate: new Date(2099, 12, 31).getTime(),
    currentDate: new Date().getTime(),
    show: false,
    currentChoose: ''
  },
  openPicker() {
    this.setData({ show: true })
  },
  onConfirm(e) {
    this.setData({ show: false, currentChoose: this.formatDate(new Date(e.detail)) })
  },
  onClose() {
    this.setData({ show: false })
  },
  onCancel() {
    this.setData({ show: false })
  },
  formatDate(date) {
    let taskStartTime
    if (date.getMonth() < 9) {
      taskStartTime = date.getFullYear() + "-0" + (date.getMonth() + 1) + "-"
    } else {
      taskStartTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
    }
    if (date.getDate() < 10) {
      taskStartTime += "0" + date.getDate()
    } else {
      taskStartTime += date.getDate()
    }
    taskStartTime += " " + date.getHours() + ":" + date.getMinutes()
    this.setData({
      taskStartTime: taskStartTime,
    })
    return taskStartTime;
  },
})

test.json

{
  "usingComponents": {
    "van-datetime-picker": "@vant/weapp/datetime-picker/index",
    "van-action-sheet": "@vant/weapp/action-sheet/index"
  }
}

结果:
点击打开选择器的时候
日期选择器的组件会从底部弹框弹出
可以选择自己想要的时间,然后将时间显示在页面上
或者传递给后端都可以
根据自己的需求进行修改~~~

以上能够使用年月日时分的组件了
有的时候
项目上会遇到这样的需求
需要将当前的时间默认显示出来
微信小程序日期选择器显示当前系统年月日时分

其实很简单
在前面的文章里面就已经提到了
参考之前写的文章
我们这里需要用到utils

可以将一些公共的代码抽离成为一个单独的 js (utils.js)文件,作为一个模块;
模块只有通过 module.exports 或者 exports 才能对外暴露接口。
所以当你在util.js里封装的方法想要在外部使用的话,必须通过 module.exports 或者 exports 对外暴露
这就和CommonJS 模块化标准一致

参考一下之前写的
微信小程序显示当前系统年月日时分秒
https://blog.csdn.net/qq_36538012/article/details/108143274

1:打开项目里面默认生成的util.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].map(formatNumber).join(':')}`
}

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

module.exports = {
  formatTime
}

2:开始写代码
打开文章上面开始写好的test.wxml的demo,写一个可以显示时间的标签

当前选择:{{currentChoose}}

3:最重要的是index.js的代码
要引入上面默认的util.js
在调用函数时,传入new Date()参数,返回值是日期和时间
再通过setData更改Page()里面的data,动态更新页面的数据

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

// 调用函数时,传入new Date()参数,返回值是日期和时间
// 再通过setData更改Page()里面的data,动态更新页面的数据

 onLoad: function() {
    // 调用函数时,传入new Date()参数,返回值是日期和时间
    var time = util.formatTime(new Date());
    // 再通过setData更改Page()里面的data,动态更新页面的数据
    this.setData({
      currentChoose: time
    });
  },

4:demo例子
test.wxml例子

当前选择:{{currentChoose}}


  

test.js例子

var util = require('../../utils/util.js');
const app = getApp()
Page({
  data: {
    minHour: 0,
    maxHour: 24,
    minDate: new Date(1990, 1, 1).getTime(),
    maxDate: new Date(2099, 12, 31).getTime(),
    currentDate: new Date().getTime(),
    show: false,
    currentChoose: ''
  },
  onLoad: function() {
    // 调用函数时,传入new Date()参数,返回值是日期和时间
    var time = util.formatTime(new Date());
    // 再通过setData更改Page()里面的data,动态更新页面的数据
    this.setData({
      currentChoose: time
    });
  },

  openPicker() {
    this.setData({
      show: true
    })
  },
  onConfirm(e) {
    this.setData({
      show: false,
      currentChoose: this.formatDate(new Date(e.detail))
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  onCancel() {
    this.setData({
      show: false
    })
  },
  formatDate(date) {
    let taskStartTime
    if (date.getMonth() < 9) {
      taskStartTime = date.getFullYear() + "-0" + (date.getMonth() + 1) + "-"
    } else {
      taskStartTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
    }
    if (date.getDate() < 10) {
      taskStartTime += "0" + date.getDate()
    } else {
      taskStartTime += date.getDate()
    }
    taskStartTime += " " + date.getHours() + ":" + date.getMinutes()
    this.setData({
      taskStartTime: taskStartTime,
    })
    return taskStartTime;
  },
})

效果显示
微信小程序日期选择器显示当前系统年月日时分
完成


你可能感兴趣的:(微信小程序日期选择器显示当前系统年月日时分)