ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 普通格式化 ``` (function(window){ /*2018年01月04日*/ var time1 = function(val) { var str = val.split(' ')[0].split('-'); return str[0] + '年' + str[1] + '月' + str[2] + '日' } /*2018.01.04*/ var time2 = function(val) { var str = val.split(' ')[0].split('-'); return str[0] + '.' + str[1] + '.' + str[2] } /*2018-01-04*/ var time3 = function(val) { return val.split(' ')[0]; } /*2017年11月27日 10:06*/ var time4 = function(val) { var str = val.split(' ') var strs = str[0].split('-'); var strss = str[1].split(':'); return strs[0] + '年' + strs[1] + '月' + strs[2] + '日 ' + ' ' + strss[0] + ':' + strss[1] } window.time1 = time1; window.time2 = time2; window.time3 = time3; window.time4 = time4; })(window) var time = '2018-01-04 18:49:58'; console.log(time1(time)); console.log(time2(time)); console.log(time3(time)); console.log(time4(time)); ``` ## 移动端格式化 ``` Vue.filter('fomatTime', function (valueTime) { if(valueTime){ var newData = Date.parse(new Date()); var diffTime = Math.abs(newData-valueTime); if (diffTime > 7 * 24 * 3600 * 1000) { var date = new Date(valueTime); var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; var h = date.getHours(); h = h < 10 ? ('0' + h) : h; var minute = date.getMinutes(); var second = date.getSeconds(); minute = minute < 10 ? ('1' + minute) : minute; second = second < 10 ? ('0' + second) : second; return m + '-' + d+' '+h+':'+minute; } else if (diffTime < 7 * 24 * 3600 * 1000 && diffTime > 24 * 3600 * 1000) { // //注释("一周之内"); // var time = newData - diffTime; var dayNum = Math.floor(diffTime / (24 * 60 * 60 * 1000)); return dayNum + "天前"; } else if (diffTime < 24 * 3600 * 1000 && diffTime > 3600 * 1000) { // //注释("一天之内"); // var time = newData - diffTime; var dayNum = Math.floor(diffTime / (60 * 60 * 1000)); return dayNum + "小时前"; } else if (diffTime < 3600 * 1000 && diffTime > 0) { // //注释("一小时之内"); // var time = newData - diffTime; var dayNum = Math.floor(diffTime / (60 * 1000)); return dayNum + "分钟前"; } } }); ```