The strange behavior of the date string toLocaleDateString in IE edge

  1. in IE edge, the time string obtained with toLocaleDateString behaves strangely. Ask for advice

the code is as follows:

    var tableDateValue = [
        new Date(new Date().getTime() - 3600 * 1000 * 24 * 8).toLocaleDateString("zh-Hans-CN").replace(//g, "").replace(/\/||/g, "-"),
        new Date(new Date().getTime() - 3600 * 1000 * 24 * 1).toLocaleDateString("zh-Hans-CN").replace(//g, "").replace(/\/||/g, "-")
    ];
    console.log(tableDateValue[0]);
    console.log(tableDateValue[1]);
    var params = {
        startTime: specifyTime(tableDateValue[0]),
        endTime: specifyTime(tableDateValue[1])
    };
    console.log(params);
    function specifyTime(time) {
        var arr = time.split("-");
        console.log(arr);
        console.log(typeof arr[1]);
        console.log(arr[1]);
        console.log(arr[1].length);
        console.log(Number(arr[1]));
        if (arr[1].length < 2 && Number(arr[1]) < 10) {
            arr[1] = "0" + arr[1];
        }
        console.log(arr[1]);
        if (arr[2].length < 2 && Number(arr[2]) < 10) {
            arr[2] = "0" + arr[2];
        }
        return arr.join("-");
    }
    
    
    arr[1].length 3   IE9IE10IE edge~~

Jun.17,2021

there may be problems with toLocaleDateString () in IE11
print:

let str = tableDateValue[0]
for(var i=0;i<str.length;iPP)console.log(i,str.charCodeAt(i),str.charAt(i))

here is the solution
toLocaleDateString (). Replace (/ u200E and gjigger')
see if you can solve your problem


you are

on this question.
toLocaleDateString('zh-Hans-CN').replace(//g, '').replace(/\/||/g, '-')
When

(compatible) there is already a problem
Chinese print out xxxx xx month xx day
ie edge the length is already 15 characters, even if you finally replace it with xxxx-xx-xx, this length remains the same.
even if the type is the same, you feel the same when you output, but you use

console.log(arr[1].charAt(0));

print and you can see that [0 points to a blank character] [1 points to 9] [2 points to white space]

toLocaleDateString('en').replace(/[^ -~]/g,'').replace(/(\d)\/(\d)\/(\d{4})/g,"$3-$2-$1"),

this personal test is possible

the most important thing is replace (/ [^-~] / gheroine')

personally recommend that you write a function of formatting time yourself

to add, / [^-~] / many people ask what this is. This is a regular expression that matches all the characters except [spaces to ~]. You can just take a look at the ASCII code (I looked it up on the Internet, but I really can't find [^-~] )


also read the answers of the previous two, have a train of thought, so the transformation is normal. Thank you for your ideas (verified by yourself, no problem)

var tableDateValue = [

]
        new Date(new Date().getTime() - 3600 * 1000 * 24 * 8).toLocaleDateString('zh-Hans-CN').replace(//g, '').replace(/\/||/g, '-').replace(/[^\d-]/g,''),
        new Date(new Date().getTime() - 3600 * 1000 * 24 * 1).toLocaleDateString('zh-Hans-CN').replace(//g, '').replace(/\/||/g, '-').replace(/[^\d-]/g,'')
    ];
Menu