安装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
;
转载请注明原文地址:https://tech.qufami.com/read-15490.html