三月
五日
前端获取数据并保存本地
function getDecodeType(value) {
localStorage.setItem("decode",value);
$.ajax({
type: 'get',
url: '*/getDecodeType',
dataType: 'json',
contentType: "application/json",
async: false,
success: function (json) {
//将后端返回来的json格式的数据转换为String格式
var str_jsonData = JSON.stringify(json)
//通过localStorage保存到本地中,命名为localData
localStorage.setItem("localData",str_jsonData);
}
});
}
function decodeTypeConvert(value) {
//读取本地的字符串数据
var getLocalData = localStorage.getItem('localData');
//显示类型 这个时候是字符串类型
console.log(typeof (getLocalData));
//将字符串类转换为json格式
var jsonObj = JSON.parse(getLocalData);
//从jsonObj中提取我们想要的数据
var data = jsonObj.decodeTypeList;
console.log(data);
for (var i = 0; i <data.length ; i++) {
if(value == data[i].optionValue){
value = data[i].optionName;
}
}
return value;
}
四月
九日
js把字符串(yyyymmdd)转换成日期格式(yyyy-mm-dd)
方法一:
var value = "20210409";
value = value.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1-$2-$3")
方法二:
var value = '20210409';
var pattern = /(\d{4})(\d{2})(\d{2})/;
var date = value.replace(pattern, '$1-$2-$3');
console.log(date);
方法三:
var value = "20210409";
if(value.length == 8){
return value.substring(0, 4) + "-" + value.substring(4, 6) + "-" + value.substring(6, 8);
} else if(value.length == 6){
return value.substring(0, 4) + "-" + value.substring(4, 6);
} else {
return value;
}