将时间戳转化为年月日
var da = 1402233166999;
da = new Date(da);
var year = da.getFullYear()+'年';
var month = da.getMonth()+1+'月';
var date = da.getDate()+'日';
console.log([year,month,date].join('-'));
将时间戳格式化
function timeStamp2String (time){ var datetime = new Date();
datetime.setTime(time);
var year = datetime.getFullYear();
var month = datetime.getMonth() + 1;
var date = datetime.getDate();
var hour = datetime.getHours();
if(hour<=9){
hour="0"+hour;
}
var minute = datetime.getMinutes();
if(minute<=9){
minute="0"+minute;
}
var second = datetime.getSeconds();
if(second<=9){
second="0"+second;
}
// var mseconds = datetime.getMilliseconds();
return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;//+"."+mseconds;
};
timeStamp2String(1608367067525);
//"2020-12-19 16:37:47"
字符串截取
function dateStr(str){
var y = str.substr(0,4);
var m = str.substr(4,2);
var d = str.substr(6,2);
var h = str.substr(8,2);
var mm =str.substr(10,2);
var sec = str.substr(12,2);
return y+'-'+m+'-'+d+' '+h+':'+mm+':'+sec;
}
dateStr("20201219165745");
"2020-12-19 16:57:45"
数字字符串的相互转换
var num = 123;
var num1 = 123;
var num2 = num.toString();
var num3 = parseInt(num2);
console.log(num1===num3);
console.log(typeof num1);
console.log(typeof num2);
console.log(typeof num3);
// true
// number
// string
// number
字符串截取后四位数
方法一
var str = '1234567';
console.log(str.substr(str.length - 4));
方法二
var x = '123456789';
x.slice(-4)
"6789"