Requirements:

Time string formatting:

  • let time = ‘2020/3/11 14:10:0’;
  • Convert the above type of time and date string to the desired format
    • =>” 14:10:00 March 11, 2020″

I. Regular processing (excellent)

(proto= > {
    function formatTime(template = '{0} year {1} Month {2} day {3} {4} minutes {5} seconds') {
        let arr = this.match(/\d+/g);
        return template.replace(/\{(\d+)\}/g, (_, n) => {
            let item = arr[n] || '0';
            item.length < 2 ? item = '0' + item : null;
            returnitem; }); } proto.formatTime = formatTime; }) (String.prototype);

let time = 'the 2020-3-11 14:10:0';
console.log(time.formatTime());//=> 14:10:00 on 11 March 2020
console.log(time.formatTime('{1} - {2} {3}, {4}'));/ / = > 3-11 languages
console.log(time.formatTime('{0} year {1} Month {2} day '));//=> 11 March 2020
Copy the code
  • Advantages: Flexible, versatile (encapsulates a common, versatile method of time formatting)

Ii. String interception (good)

let time = '2020/3/11 14:10:0';

/* 1. Get all the values representing time in the original string, and finally concatenate them into what we want */
let arr = time.split(' '); / / = > [" 2020/3/11 ", "14:10:0"]
let arrLeft = arr[0].split('/'); / / = > [" 2020 ", "3", "11"]
let arrRight = arr[1].split(':'); / / = > [" 14 ", "10", "0"]
arr = arrLeft.concat(arrRight); //=>["2020", "3", "11", "14", "10", "0"]

Before splicing, add zero before ARRLEFT and ARRRIGHT
arr = arr.map(item= > item.length < 2 ? '0' + item : item);
time = `${arr[0]}years${arr[1]}month${arr[2]}day${arr[3]}when${arr[4]}points${arr[5]}Second `;

console.log(time);//=>"2020 March 11 14:10:00"
Copy the code
  • Advantages and disadvantages: simple ideas, but not flexible enough

String Interception combined with Simple Re processing (1)

let time = '2020/3/11 14:10:0';

let arr = time.match(/\d+/g); //=>["2020", "3", "11", "14", "10", "0"]
arr = arr.map(item= > item.length < 2 ? '0' + item : item);
time = `${arr[0]}years${arr[1]}month${arr[2]}day${arr[3]}when${arr[4]}points${arr[5]}Second `;

console.log(time);//=> 14:10:00 on 11 March 2020
Copy the code
  • inflexible

String Interception combined with Simple Regular Processing (2)

let time = '2020/3/11 14:10:0';

// Operations with less than ten zeros are encapsulated as a method
function zero(val) {
    return val.length < 2 ? '0' + val : val;
}

let arr = time.split(/ (? : |\/|:)/g); //=>["2020", "3", "11", "14", "10", "0"]
time = `${arr[0]}years${zero(arr[1])}month${zero(arr[2])}day${zero(arr[3])}when${zero(arr[4])}points${zero(arr[5])}Second `;

console.log(time);//=> 14:10:00 on 11 March 2020
Copy the code
  • inflexible

In some parts of the world, some are replaced by others.

let time = '2020/3/11 14:10:0';

time = time.replace('/'.'years').replace('/'.'month').replace(' '.'day').replace(':'.'when').replace(':'.'points') + '秒';

console.log(time); //=> 14:10:00 on 11 March 2020
Copy the code
  • Disadvantages: no supplementary zeros for less than ten digit numbers

Mind mapping