处理格式化时间后 时区误差8小时的问题


 1 function formatTime(mdate, fmt) {
 2 if (fmt == undefined) {
 3 fmt = 'yyyy-MM-dd hh:mm:ss'
 4 }
 5 if (mdate == '') {
 6 return ''
 7 }
 8 var date = new Date((mdate+"").replace("T", " ").replace(/-/g,'/'));   //这里处理的时候需要注意 苹果IOS 不支持 2020-12-10 08:12:30  这种形式转换为Date类型 必须转换为 2020/12/10 08:12:30 的形式
 9 if (/(y+)/.test(fmt)) {
10 fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
11 }
12 let o = {
13 'M+': date.getMonth() + 1,
14 'd+': date.getDate(),
15 'h+': date.getHours(),
16 'm+': date.getMinutes(),
17 's+': date.getSeconds()
18 };
19 for (let k in o) {
20 if (new RegExp(`(${k})`).test(fmt)) {
21 let str = o[k] + '';
22 fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length));
23 }
24 }
25 return fmt;
26 }

 

 

1.如果时间中间带有T 符号,在转换为时间会多8小时 我们需要首先 将字符串中的T 替换掉

2.苹果IOS系统不支持 将 2020-12-12 00:00:00 格式的字符串直接转换为时间 必须使用 2020/12/12 00:00:00 形式的字符串转换为时间格式

所以使用 上面代码框中的方法是可以实现 兼容性的方法

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM