这里是一个毫秒转分:秒的格式化函数,format2,如何在云函数中调用?
// 云函数入口文件
const cloud = require('wx-server-sdk')
const dateUtils = require('date-utils')
process.env.TZ ='Asia/Shanghai'
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
console.log(event);
let padTo2Digits = function (num) {
return num.toString().padStart(2, '0');
}
let padTo3Digits = function (num) {
return num.toString().padStart(3, '0');
}
let format2 = function(num){
let totalSeconds = num;
let ms = totalSeconds % 1000;
totalSeconds = parseInt(num/1000);
// ️ 获取完整分钟数
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
// ✅ 格式化为 MM:SS
// const result = `${this.padTo2Digits(minutes)}:${this.padTo2Digits(seconds)}:${this.padTo3Digits(ms)}`;
const result = `${this.padTo2Digits(minutes)}:${this.padTo2Digits(seconds)}`;
//console.log(result); // ️ "09:25"
return result;
}
try {
let dt = new Date();
let today = dt.toFormat("YYYY-MM-DD");
let time = dt.toFormat("YYYY-MM-DD HH24:MI:SS");
let res = await db.collection('profiles')
.aggregate()
.match({
_id:_.exists(true)
})
.skip(0)
.limit(500)
.end()
let items = res.list;
items.forEach(async (item)=>{
let _id = item['_id'];
let diff = item['diff'];
let ytimes = this.format2(diff);
console.log(_id,ytimes);
await db.collection('profiles').doc(_id).update({
// data 字段表示需新增的 JSON 数据
data: {
ytimes: ytimes,
}
})
})
} catch(e) {
console.error(e)
}
}
你要看下format2 是在哪个文件里面,是怎么引入的,举个栗子如果是在date-utils里面,那么
const dateUtils = require('date-utils');
dateUtils.format2 (xxx)
let ytimes = format2(diff);