- const models = {
- //格式化CST日期的字串
- formatCSTDate: function(strDate, format) {
- return this.formatDate(new Date(strDate), format);
- },
- //格式化日期,
- formatDate: function(date, format) {
- var paddNum = function(num) {
- num += "";
- return num.replace(/^(\d)$/, "0$1");
- }
- //指定格式字符
- var cfg = {
- yyyy: date.getFullYear() //年 : 4位
- ,
- yy: date.getFullYear().toString().substring(2) //年 : 2位
- ,
- M: date.getMonth() + 1 //月 : 如果1位的时候不补0
- ,
- MM: paddNum(date.getMonth() + 1) //月 : 如果1位的时候补0
- ,
- d: date.getDate() //日 : 如果1位的时候不补0
- ,
- dd: paddNum(date.getDate()) //日 : 如果1位的时候补0
- ,
- hh: date.getHours() //时
- ,
- mm: date.getMinutes() //分
- ,
- ss: date.getSeconds() //秒
- }
- format || (format = "yyyy-MM-dd hh:mm:ss");
- return format.replace(/([a-z])(\1)*/ig,
- function(m) {
- return cfg[m];
- });
- }
- }
- export
- default models;
以上将字符串和日期的操作方法都放在模块中,方便调用。
将写好的模块通过 import 关键字引入,然后调用该模块中对应的方法实现相应的功能。
来源: http://www.2cto.com/kf/201704/626514.html