Format time zone time
Hello, long time no see. Today, during development, I encountered a time format data returned from the background. I originally wanted to use dayjs to format it, but I didn’t have the brain in the end, so I came up with the following function, use it quickly!
The backend returned a data of this type:
Sat Sep 24 2022 11:36:12 GMT+0800 (China Standard Time)
For such a lazy backend, we don’t have to complain, we can solve it by ourselves.
So let's get started.
-
utils–format.js
first creates a new format.js folder under our utils file
-
Then enter the following code:
//格式化时间的方法
let dateConversion = (dateValue) => {
let date = new Date(dateValue);
let seperator1 = '-';
let seperator2 = ':';
let month = date.getMonth() + 1;
let strDate = date.getDate();
// 时分秒
let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
let minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
let second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return (
date.getFullYear() +
seperator1 +
month +
seperator1 +
strDate +
' ' +
hour +
seperator2 +
minute +
seperator2 +
second
);
};
export default dateConversion;
- Finally, just introduce and use it in the interface we need to use.
//引入
import dateConversion from '/@/utils/Timeformat/format';
//使用:
dateConversion('Sun Oct 09 2022 17:16:19 GMT+0800 (中国标准时间)')
//---log出来试试看吧!---
result: