博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
计算时间差,页面倒计时,安卓与ios兼容问题
阅读量:6898 次
发布时间:2019-06-27

本文共 1110 字,大约阅读时间需要 3 分钟。

前言

在开发一些有关商品交易类的项目时,多半会遇到活动倒计时之类的需求,最近也是在小程序中遇到,实现方法很多,但是在小程序中遇到ios和安卓的兼容问题,所以记录下来

代码

/**   * timestampSwitch - 根据对比传入的两个时间戳,计算出相差的时分秒   *   * @param{String}startTimestamp 计算起始时间戳,默认是当前时间   * @param{Number}endTimestamp 计算结束时间(当前接受的是时间字符串,如2018-11-30 23:59:59)   * @return{Object}   */  const timestampSwitch = (endTimestamp, startTimestamp = (new Date()).valueOf()) => {    if (!Number(endTimestamp) || !Number(startTimestamp)) console.error('Incorrect parameter');    // 兼容ios    let et = Date.parse(endTimestamp) || Date.parse(endTimestamp.replace(/-/g, '/'));    // 计算    let difference = (endTimestamp - startTimestamp),        timeDifference = (difference > 0 ? difference : 0) / 1000,        days = parseInt(timeDifference / 86400),        hours = parseInt((timeDifference % 86400) / 3600),        minutes = parseInt((timeDifference % 3600) / 60),        seconds = parseInt(timeDifference % 60);    return {      days,      hours,      minutes,      seconds    }  };

问题

问题在于后台给我的是时间字符串,我们需要转为时间戳后计算,但是安卓和ios转换时会有不同如上代码

iOSDate.parse(endTimestamp)转为时间戳会报错,兼容性方法Date.parse(endTimestamp.replace(/-/g, '/'))

转载地址:http://sycdl.baihongyu.com/

你可能感兴趣的文章
Struts2的配置
查看>>
[BZOJ1296][SCOI2009]粉刷匠(DP)
查看>>
Executor执行框架
查看>>
[FMX] Android APP 启动黑屏优化补丁
查看>>
常用JavaScript的高级技巧
查看>>
bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘
查看>>
mac编辑器vim美化
查看>>
MD5摘要算法简析
查看>>
《30天自制操作系统》学习笔记一
查看>>
Python.tornado.2.tornado.options
查看>>
mysql关于or的索引问题
查看>>
装在u盘的linux
查看>>
ASP.NET几种页面数据绑定的用法及区别: <%#、 <%=、 <% 、<%@
查看>>
zookeeper
查看>>
ABP源码分析二十四:Notification
查看>>
Photo4
查看>>
(八)mybatis之多对多
查看>>
h5空白页面过渡加载
查看>>
端午悲剧—我的上海情结(二)
查看>>
Ajax实现登陆并友好提示错误信息
查看>>