Line of code to achieve time stamp format of rotation

https://segmentfault.com/a/1190000015992232?utm_source=tag-newest

 

 

function time(time = +new Date()) {
var date = new Date(time + 8 * 3600 * 1000);
return date.toJSON().substr(0, 19).replace('T', ' ').replace('/-/g', '.');
}
console.log(time());

 

 

Front-end development process, often need to be converted to standard time timestamp format for users to browse. Without the aid of the method library, faster and better how to achieve it? Here are two methods.

Old method

Usually use this method substantially, by the method sequentially Date The date when the calculated one by one every minute, and then spliced ​​into the time required for the format string.

function transformTime(timestamp = +new Date()) { if (timestamp) { var time = new Date(timestamp); var y = time.getFullYear(); //getFullYear方法以四位数字返回年份 var M = time.getMonth() + 1; // getMonth方法从 Date 对象返回月份 (0 ~ 11),返回结果需要手动加一 var d = time.getDate(); // getDate方法从 Date 对象返回一个月中的某一天 (1 ~ 31) var h = time.getHours(); // getHours方法返回 Date 对象的小时 (0 ~ 23) var m = time.getMinutes(); // getMinutes方法返回 Date 对象的分钟 (0 ~ 59) var s = time.getSeconds(); // getSeconds方法返回 Date 对象的秒数 (0 ~ 59) return y + '-' + M + '-' + d + ' ' + h + ':' + m + ':' + s; } else { return ''; } } transformTime(); // "2018-8-8 12:9:12"

Improved version of the old method

The above conversion method, the method is converted into a time stamp Date instance, when using the corresponding acquired Date date corresponding to the moment to obtain time format is '2018-8-812: 9: 12', looked somewhat awkward . For conversion of our common time format, is also noted on the value of less than 10, add the string in front of '0', is converted to '2018-08-08 12:09:12' this time format.

function transformTime(timestamp = +new Date()) { if (timestamp) { var time = new Date(timestamp); var y = time.getFullYear(); var M = time.getMonth() + 1; var d = time.getDate(); var h = time.getHours(); var m = time.getMinutes(); var s = time.getSeconds(); return y + '-' + addZero(M) + '-' + addZero(d) + ' ' + addZero(h) + ':' + addZero(m) + ':' + addZero(s); } else { return ''; } } function addZero(m) { return m < 10 ? '0' + m : m; } transformTime(); // "2018-08-08 12:09:12"

Value of less than 10 on the return time of processing, the character string adding '0', so that the format used on the symmetric 'addZero' method.

New ideas

In order to convert the timestamp format we need time, we wrote two functions, add up to a dozen lines. Some time ago, the department chiefs told another way, a line of code completion timestamp converted to 'YYYY-MM-DD HH: mm: ss' format in the form of time, suddenly a lot of streamlining the code, did not talk much, raising Code

function time(time = +new Date()) { var date = new Date(time + 8 * 3600 * 1000); // 增加8小时 return date.toJSON().substr(0, 19).replace('T', ' '); } time(); // "2018-08-09 18:25:54"

Date of 'toJSON' method JSON format string GMT, the actual result of using 'toISOString' method. The shaped string '2018-08-09T10: 20: 54.396Z', into eight additional need GMT time zone, we need to take the string 19 before, then the 'T' replaced with spaces, that is, we need time format.

function time(time = +new Date()) { var date = new Date(time + 8 * 3600 * 1000); return date.toJSON().substr(0, 19).replace('T', ' ').replace(/-/g, '.'); } time(); // "2018.08.09 18:25:54"

The time format '-' '.' Or modify other symbols are possible. Contrast the old method, which codes than before saving more than one star the slightest, and also read a lot cleaner. If the time format requires a few milliseconds, only needs to acquire the first 23-digit string, and replaced with the replace method as above.

Guess you like

Origin www.cnblogs.com/maidongdong/p/11230988.html
Recommended