時(shí)間戳轉(zhuǎn)日期
function add0(m) { return m < 10 ? '0' + m : m } // 加零函數(shù)
function formatDate(g) {
let now = new Date(g); // 創(chuàng)建一個(gè)指定的日期對(duì)象
let year = now.getFullYear(); // 取得4位數(shù)的年份
let month = now.getMonth() + 1 ; // 取得日期中的月份,其中0表示1月冰肴,11表示12月
let date = now.getDate(); // 返回日期月份中的天數(shù)(1到31)
let hour = now.getHours(); // 返回日期中的小時(shí)數(shù)(0到23)
let minute = now.getMinutes(); // 返回日期中的分鐘數(shù)(0到59)
let second = now.getSeconds(); // 返回日期中的秒數(shù)(0到59)
return `${year}-${add0(month)}-${add0(date)} ${add0(hour)}:${add0(minute)}:${add0(second)}`
}
let g = new Date().getTime(); // 定義一個(gè)時(shí)間戳變量
console.log(formatDate(g));
HTML特殊轉(zhuǎn)義字符解譯
function htmlDecode(text) {
//1.首先動(dòng)態(tài)創(chuàng)建一個(gè)容器標(biāo)簽元素取董,如DIV
var temp = document.createElement("div");
//2.然后將要轉(zhuǎn)換的字符串設(shè)置為這個(gè)元素的innerHTML(ie程帕,火狐太惠,google都支持)
temp.innerHTML = text;
//3.最后返回這個(gè)元素的innerText(ie支持)或者textContent(火狐矿酵,google支持),即得到經(jīng)過(guò)HTML解碼的字符串了。
var output = temp.innerText || temp.textContent;
temp = null;
return output;
},