egg.js工具类helper.js中时间戳转换为日期格式方法封装

tech2024-03-21  64

安装moment依赖

npm -i moment -save

创建文件 app/extends/helper.js

'use strict'; const moment = require('moment'); exports.relativeTime = (time, format) => { return moment(time).format(format); };

创建控制器app/controller/helper.js

'use strict'; const Controller = require('egg').Controller; const { relativeTime } = require('../extend/helper'); class HelperCeshiController extends Controller { async momentTimeCeshi() { const currentTime = new Date(); const rs = {}; rs.ymdhms = relativeTime(currentTime, 'YYYY-MM-DD HH:mm:ss'); rs.ymd = relativeTime(currentTime, 'YYYY-MM-DD'); rs.ymdx = relativeTime(currentTime, 'YYYY/MM/DD'); this.ctx.body = rs; } } module.exports = HelperCeshiController;
最新回复(0)