微信小程序获取当前时间戳、日期、时间

获取时间戳,注意在微信小程序按照时间戳对数据库内容排序时,orderBy的第一个参数类型为Number

	var timeStamp = Date.parse(new Date());  
	timeStamp = timeStamp / 1000;  
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  var value={}
  console.log(value)
  const db=cloud.database()
  await db.collection('article')
  .orderBy("timestamp",'desc')
  .get()
  .then(res=>{
    console.log(res.data)
    value.data=res.data
    console.log(value)
  })
  return value
}

获取当前日期、时间并连接成如2022-04-19 19:40:10样式

	var blank=""
    var myDate = new Date();
    var year=myDate.getFullYear();
    var month=(myDate.getMonth() + 1 < 10 ? '0' + (myDate.getMonth() + 1) : myDate.getMonth() + 1);
    var date=myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate();
    var hour=myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours();
    var min=myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes();
    var sec=myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds();
    var myTime=blank.concat(year,"-",month,"-",date," ",hour,":",min,":",sec);

你可能感兴趣的:(微信小程序,微信小程序)