温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ios中getTime()的兼容性实例代码

发布时间:2020-09-15 20:17:24 来源:脚本之家 阅读:152 作者:201818 栏目:移动开发

时间格式为:2017-12-12 12:00:00在苹果上获取时间戳有兼容性问题

需要转换成2017/12/12 12:00:00 才可以正确获取到时间戳

   let u = navigator.userAgent;  //判断浏览器型号
   let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;  //android终端
   let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
   if(isiOS){
    let time = new Date((v[0].createTime).replace(/-/g,'/')).getTime();
     this.timeago=this.getDateDiff(time);
   }else{
    let time = new Date(v[0].createTime).getTime();
     this.timeago=this.getDateDiff(time);
   }
判断时间过去了多久
  getDateDiff(dateTimeStamp){
  let result;
  let minute = 1000 * 60;
  let hour = minute * 60;
  let day = hour * 24;
  let halfamonth = day * 15;
  let month = day * 30;
  let now = new Date().getTime();
  let diffValue = now - dateTimeStamp;
  if(diffValue < 0){
   return;
  }
  let monthC =diffValue/month;
  let weekC =diffValue/(7*day);
  let dayC =diffValue/day;
  let hourC =diffValue/hour;
  let minC =diffValue/minute;
  if(monthC>=1){
   if(monthC<=12)
    result="" + parseInt(monthC) + "月前";
   else{
    result="" + parseInt(monthC/12) + "年前";
   }
  }
  else if(weekC>=1){
   result="" + parseInt(weekC) + "周前";
  }
  else if(dayC>=1){
   result=""+ parseInt(dayC) +"天前";
  }
  else if(hourC>=1){
   result=""+ parseInt(hourC) +"小时前";
  }
  else if(minC>=1){
   result=""+ parseInt(minC) +"分钟前";
  }else{
   result="刚刚";
  }
  return result;
 },

到此这篇关于ios中getTime()的兼容性实例代码的文章就介绍到这了,更多相关getTime()的兼容性内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI