itheima-utils-timecarol
Version:
提供了格式化时间, HTMLEscape的功能
33 lines (32 loc) • 1.38 kB
JavaScript
module.exports.dateFormat = function (date) {
date = date || new Date();
const year= date.getFullYear();
const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth();
const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
const hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
const minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
const second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return [year, '-', month , '-', day, ' ', hour , ':', minute, ':', second ].join('');
};
module.exports.htmlEncode = function (str) {
let temp = "";
if (str.length === 0) return "";
temp = str.replace(/&/g,"&");
temp = temp.replace(/</g,"<");
temp = temp.replace(/>/g,">");
temp = temp.replace(/\s/g," ");
temp = temp.replace(/\'/g,"'");
temp = temp.replace(/\"/g,""");
return temp;
};
module.exports.htmlDecode = function (str) {
let temp = "";
if(str.length === 0) return "";
temp = str.replace(/&/g,"&");
temp = temp.replace(/</g,"<");
temp = temp.replace(/>/g,">");
temp = temp.replace(/ /g," ");
temp = temp.replace(/'/g,"\'");
temp = temp.replace(/"/g,"\"");
return temp;
};