1.首先先在云数据库创造一个集合test

2.在wxml中(以下为一个模拟)

//点击获取时间
<button bindtype="gettime">点击获取时间</button>

 3.接下来是js部分

Page
({

 gettime() {
  var that = this;
  var nowtime = new Date().getTime()
wx.cloud.database().collection('test').add({
data:{
//在云数据库test集合中添加当前时间
time:that.formatDate(nowtime)
},
success(res) {
 console.log("添加成功",res)
},
fail(res) {
console.log("添加失败",res)
}
})
},


//获取时间函数,

  formatDate: function (times) {
      var date = new Date(times);
      var year = date.getFullYear(); //年份
      var month = date.getMonth() + 1; //月份
      var day = date.getDate(); //日
      var hour = function () { //获取小时
          return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
      }
      var minute = function () { //获取分钟
          return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
      }

      var second = function () { //获取秒数
          return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
      }
      return year + '-' + month + '-' + day +' '+hour+':'+minute+':'+second
      

  },

})

 

如上,在数据库中添加例如2021-4-23 16:12:55 这种数据类型的某刻时间 (这里我在return中删除了hour+minute+second)

第二部分:按时间排序

需要用到.orderBy('time','desc') 

其中time为云数据库的一条记录。desc为从大到小排序,asc则相反

只需要在获取数据库数据时候加上orderby即可 

 

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐