Time format conversion

A time format conversion request was recently encountered in the project.

Conversion rules

  1. If the timestamps (the timestamps mentioned below all point to the timestamps to be converted) are today, the format returned is "HH:mm:ss"
  2. If the timestamp is yesterday, the format returned is "yesterday HH:mm:ss"
  3. If the time stamp is within this week and not yesterday, if it is Monday, the format returned is "Monday HH:mm:ss"
  4. If the time stamp is not a week in the current year, if it is January 12, the format returned is "January 12 HH:mm:ss"
  5. If the time stamp is within the date before this year, the format returned is "yyyy mm MM dd day HH:mm:ss"

Example

There are also some on the Internet, but the details are different from the rules, so I wrote one myself. You can click here to experience it first: See

Code

function timeChangeFormat(timeObj) {
    timeObj = timeObj * 1;
    var curTime = new Date().getTime(); // Current timestamp
    var curTimeMinutes = parseInt(curTime / 1000 / 60); //Current time minutes
    var timeObjMinutes = parseInt(timeObj / 1000 / 60); //Parameter time minutes
    var timeDifference = curTimeMinutes - timeObjMinutes; // Parameter timestamp differs from current timestamp by minutes

    var curDate = new Date(curTime); // Current time date object
    var timeObjDate = new Date(timeObj); // Date object converted from parameter timestamp

    var timeYear = timeObjDate.getFullYear(),
        timeMonth = timeObjDate.getMonth() + 1,
        timeDate = timeObjDate.getDate(),
        timeDay = timeObjDate.getDay(),
        timeHour = timeObjDate.getHours(),
        timeMinutes = timeObjDate.getMinutes(),
        timeSeconds = timeObjDate.getSeconds();
    var weekDayText = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    // Complement 0 operation
    function addZero(num) {
        return (String(num).length == 1 ? '0' : '') + num;
    }
    // Whether the time stamp of the transformation is in the same week as the current time returns true, otherwise false
    function isSameWeek(curDate, timeDifference) {
        var day = curDate.getDay() - 1;
        var hour = curDate.getHours();
        var minutes = curDate.getMinutes();
        var weekMinutes = day * 24 * 60 + hour * 60 + minutes; // How many minutes have passed since the current time
        if (timeDifference <= weekMinutes) {
            return true;
        } else {
            return false;
        }
    }

    if (curDate.getFullYear() == timeYear && curDate.getMonth() + 1 == timeMonth && curDate.getDate() == timeDate) {
        return addZero(timeHour) + ':' + addZero(timeMinutes) + ':' + addZero(timeSeconds);
    } else {
        var newDate = new Date((curTimeMinutes - 1440) * 60 * 1000);
        if (newDate.getFullYear() == timeYear && newDate.getMonth() + 1 == timeMonth && newDate.getDate() == timeDate) {
            return 'Yesterday ' + addZero(timeHour) + ':' + addZero(timeMinutes) + ':' + addZero(timeSeconds);
        } else if (isSameWeek(curDate, timeDifference)) {
            return weekDayText[timeDay] + ' ' + addZero(timeHour) + ':' + addZero(timeMinutes) + ':' + addZero(timeSeconds);
        } else if (curDate.getFullYear() == timeYear) {
            return addZero(timeMonth) + 'month' + addZero(timeDate) + 'day ' + addZero(timeHour) + ':' + addZero(timeMinutes) + ':' + addZero(timeSeconds);
        } else {
            return timeYear + 'year' + addZero(timeMonth) + 'month' + addZero(timeDate) + 'day ' + addZero(timeHour) + ':' + addZero(timeMinutes) + ':' + addZero(timeSeconds);
        }
    }
}

Parameter description

The parameter of the function is a millisecond time stamp.

Posted on Sun, 03 May 2020 03:27:45 -0400 by richard-elland