UNPKG

itheima-utils-timecarol

Version:

提供了格式化时间, HTMLEscape的功能

33 lines (32 loc) 1.38 kB
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,"&amp;"); temp = temp.replace(/</g,"&lt;"); temp = temp.replace(/>/g,"&gt;"); temp = temp.replace(/\s/g,"&nbsp;"); temp = temp.replace(/\'/g,"&#39;"); temp = temp.replace(/\"/g,"&quot;"); return temp; }; module.exports.htmlDecode = function (str) { let temp = ""; if(str.length === 0) return ""; temp = str.replace(/&amp;/g,"&"); temp = temp.replace(/&lt;/g,"<"); temp = temp.replace(/&gt;/g,">"); temp = temp.replace(/&nbsp;/g," "); temp = temp.replace(/&#39;/g,"\'"); temp = temp.replace(/&quot;/g,"\""); return temp; };