0
点赞
收藏
分享

微信扫一扫

微信小程序IOS系统日期的问题

逸省 2022-08-31 阅读 54


最近在写微信端小程序的时候,有涉及到通过日期字符查询,原有代码如下:

// 当天0点 
startTime1 = Date.parse(
new Date(new Date(new Date().toLocaleDateString()).getTime())
);
// 当天23:59分
endTime1 = Date.parse(
new Date(
new Date(new Date().toLocaleDateString()).getTime() +
24 * 60 * 60 * 1000 -
1
)
);

该代码在IOS12版本真机上测试都没有问题,startTime1,endTime1都能正常转换成Unix时间戳。

但在IOS8版本真机上测试去碰到转换出错的情况,跟踪调试转换结果为"NaN"。

网上搜索得到以下信息,"使用日期连接符时,因为ios不兼容,所以不要使用‘YYYY-MM-DD’,需要使用‘YYYY/MM/DD’ “

 根据以上情况分析,怀疑IOS8不支持toLocaleDateString()取值,将以上代码更改如下:

//自定义获取日期函数
myDate(){
var date= new Date();
return date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate()
}

// 当天0点
startTime1 = Date.parse(
new Date(new Date(this.myDate()).getTime())
);
// 当天23点59分
endTime1 = Date.parse(
new Date(
new Date(this.myDate()).getTime() +
24 * 60 * 60 * 1000 -
1
)
);

以上代码在安卓,IOS12,IOS8真机测试通过。 

 

举报

相关推荐

0 条评论