js把字符串(yyyymmdd)转换成日期格式(yyyy-mm-dd)

1.

var r="20112233".replace(/^(\d{4})(\d{2})(\d{2})$/, "$1-$2-$3")
console.log(r)

2.

var dateString = '19930701';
var pattern = /(\d{4})(\d{2})(\d{2})/;
var formatedDate = dateString.replace(pattern, '$1-$2-$3');
console.log(formatedDate);

3. 

function formatStringyyyyMMddToyyyy_MM_dd (value){
    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;
    }
}

 

你可能感兴趣的:(js)