gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章ios中getTime()的兼容性实例代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

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

?
1
2
3
4
5
6
7
8
9
10
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);
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
判断时间过去了多久
   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()的兼容性内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

原文链接:https://juejin.im/post/5b42cbaef265da0fa009fd31 。

最后此篇关于ios中getTime()的兼容性实例代码的文章就讲到这里了,如果你想了解更多关于ios中getTime()的兼容性实例代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com