Iphone端Javascript new Date(Time)报错:invalid date

本文介绍了一种在iOS设备上解决日期格式不被正确解析的问题。通过简单的字符串替换方法,可以将不兼容的日期格式(如使用斜杠或横杠分隔的日期)转换为iOS能够识别的格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 代码:
    var end = new Date(‘2017/12/14 12:35:58’).getTime();
    or
    var end = new Date(‘2017-12-14 12:35:58’).getTime();


  • 报错:
    执行上面两种代码时,在PC端测试没有问题,但是放到Iphone端测试发现:alert(end ),打印的是invalid date,没有正确创建时间对象;


  • 原因:
    在Iphone端不能识别“/”或者“-”,这是需要将“/”改为“-”;或者将“-”改为“/”;


  • 解决方法:
    如果是/格式就转换成-,反之就改成/;
    //日期横杠=>斜杠
    function exchange(date){
    var time = “’”+date+”’”;
    var temp = time.replace(/-/g,”/”).substring(0,time.lastIndexOf(“.”));
    return temp;
    }
    //日期”/”=>”-”
    function exchange(date){
    var time = “’”+date+”’”;
    var temp = time.replace(/\//g,”-“).substring(0,time.lastIndexOf(“.”));
    return temp;
    }

请对下列预警错误代码进行修复后给我完整代码。[ WXML 文件编译错误] ./pages/finance-circle/demand-release/index.wxml Bad value with message: unexpected token `Date`. 7 | <text class="nickname">{{userInfo.nickName || '未登录用户'}}</text> 8 | <text class="contact">联系方式: {{userInfo.phone ? userInfo.phone.slice(0,3) + '****' + userInfo.phone.slice(7) : '未授权'}}</text> > 9 | <text class="time">发布时间: {{utils.formatTime(new Date())}}</text> | ^ 10 | </view> 11 | </view> 12 | at files://pages/finance-circle/demand-release/index.wxml#9(env: Windows,mp,1.06.2503300; lib: 3.8.9) [渲染层错误] ReferenceError: SystemError (webviewScriptError) __route__ is not defined(env: Windows,mp,1.06.2503300; lib: 3.8.9) [渲染层错误] [jsbridge] invoke reportKeyValue fail: too eayly. Error: invoke too eayly at http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:121407 at Object.invoke (http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:118605) at B (http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:150571) at Foundation.requestIdleCallback.timeout (http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:167336)(env: Windows,mp,1.06.2503300; lib: 3.8.9) invalid app.json permission["scope.writePhotosAlbum"] [Deprecation] SharedArrayBuffer will require cross-origin isolation as of M92, around July 2021. See https://developer.chrome.com/blog/enabling-shared-array-buffer/ for more details. [system] WeChatLib: 3.8.9 (2025.6.17 19:09:25) [渲染层错误] [jsbridge] invoke reportKeyValue fail: too eayly. Error: invoke too eayly at http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:121407 at Object.invoke (http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:118605) at B (http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:150571) at Function.reportQualityData2 (http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:169342) at Object.<anonymous> (http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:173771) at Ou (http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:538108) at http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:536050 at Generator.next (<anonymous>) at n (http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:5193) at s (http://127.0.0.1:10560/__pageframe__/__dev__/WAWebview.js?t=wechat&v=3.8.9:1:5396)(env: Windows,mp,1.06.2503300; lib: 3.8.9) [system] No. of subpackages: 14 [system] LazyCodeLoading: false [wxobs] auto recording mode is not enabled in devtools. [体验分析] 为避免影响调试,开发者工具在非自定义启动模式下不会启动采集。详见 https://dev.weixin.qq.com/docs/analysis/sdk/debugging.html invalid app.json permission["scope.writePhotosAlbum"] VM2096 app.js:71 小程序初始化完成 {path: "pages/home/home", query: {…}, scene: 1001, referrerInfo: {…}, mode: "default", …} wx.getSystemInfoSync is deprecated.Please use wx.getSystemSetting/wx.getAppAuthorizeSetting/wx.getDeviceInfo/wx.getWindowInfo/wx.getAppBaseInfo instead. getSystemInfo @ VM2096 app.js:91 onLaunch @ VM2096 app.js:74 (anonymous) @ VM2096 app.js:7 VM2096 app.js:93 系统信息: {batteryLevel: 100, benchmarkLevel: -1, brand: "devtools", memorySize: 2048, model: "iPhone 12/13 (Pro)", …} [Perf] App.getSystemInfo took 53ms onLaunch @ VM2096 app.js:74 (anonymous) @ VM2096 app.js:7 VM2096 app.js:111 当前环境: develop, API地址: https://dev-api.lyqf.com VM2096 app.js:162 开始检查所有插件状态... VM2096 app.js:189 vantPlugin 插件加载成功: {answer: 42, sayHello: ƒ} VM2096 app.js:214 vantPlugin 插件加载失败: TypeError: wx.getPluginVersion is not a function at Rt.checkSinglePluginStatus (VM2096 app.js:195) at Rt.<anonymous> (VM1989 WASubContext.js:1) at VM2096 app.js:172 at Array.forEach (<anonymous>) at Rt.checkAllPluginsStatus (VM2096 app.js:168) at Rt.<anonymous> (VM1989 WASubContext.js:1) at Rt.onLaunch (VM2096 app.js:80) at Rt.<anonymous> (VM1989 WASubContext.js:1) at new Rt (VM1989 WASubContext.js:1) at t.<anonymous> (VM1989 WASubContext.js:1)(env: Windows,mp,1.06.2503300; lib: 3.8.9) checkSinglePluginStatus @ VM2096 app.js:214 (anonymous) @ VM2096 app.js:172 checkAllPluginsStatus @ VM2096 app.js:168 onLaunch @ VM2096 app.js:80 (anonymous) @ VM2096 app.js:7 VM2096 app.js:189 myPlugin 插件加载成功: {getLocation: ƒ, setLocation: ƒ} VM2096 app.js:214 myPlugin 插件加载失败: TypeError: wx.getPluginVersion is not a function at Rt.checkSinglePluginStatus (VM2096 app.js:195) at Rt.<anonymous> (VM1989 WASubContext.js:1) at VM2096 app.js:172 at Array.forEach (<anonymous>) at Rt.checkAllPluginsStatus (VM2096 app.js:168) at Rt.<anonymous> (VM1989 WASubContext.js:1) at Rt.onLaunch (VM2096 app.js:80) at Rt.<anonymous> (VM1989 WASubContext.js:1) at new Rt (VM1989 WASubContext.js:1) at t.<anonymous> (VM1989 WASubContext.js:1)(env: Windows,mp,1.06.2503300; lib: 3.8.9) checkSinglePluginStatus @ VM2096 app.js:214 (anonymous) @ VM2096 app.js:172 checkAllPluginsStatus @ VM2096 app.js:168 onLaunch @ VM2096 app.js:80 (anonymous) @ VM2096 app.js:7 VM2096 app.js:258 用户未登录 [Perf] App.checkLoginStatus took 141ms onLaunch @ VM2096 app.js:83 (anonymous) @ VM2096 app.js:7 [Perf] App.onLaunch took 272ms (anonymous) @ VM2096 app.js:7 wx.getSystemInfoSync is deprecated.Please use wx.getSystemSetting/wx.getAppAuthorizeSetting/wx.getDeviceInfo/wx.getWindowInfo/wx.getAppBaseInfo instead. getSystemInfoSync @ VM2038 version.js:11 gte @ VM2038 version.js:39 canIUseFormFieldButton @ VM2038 version.js:47 (anonymous) @ VM2047 index.js:11 invalid app.json permission["scope.writePhotosAlbum"] invalid app.json permission["scope.writePhotosAlbum"] [system] WeChatLib: 3.8.9 (2025.6.17 19:09:25) [system] No. of subpackages: 14 [system] LazyCodeLoading: false [wxobs] auto recording mode is not enabled in devtools. [体验分析] 为避免影响调试,开发者工具在非自定义启动模式下不会启动采集。详见 https://dev.weixin.qq.com/docs/analysis/sdk/debugging.html app.js? [sm]:62 小程序初始化完成 {path: "pages/home/home", query: {…}, scene: 1001, referrerInfo: {…}, mode: "default", …} wx.getSystemInfoSync is deprecated.Please use wx.getSystemSetting/wx.getAppAuthorizeSetting/wx.getDeviceInfo/wx.getWindowInfo/wx.getAppBaseInfo instead. getSystemInfo @ app.js? [sm]:83 onLaunch @ app.js? [sm]:65 (anonymous) @ app.js? [sm]:3 app.js? [sm]:85 系统信息: {batteryLevel: 100, benchmarkLevel: -1, brand: "devtools", memorySize: 2048, model: "iPhone 12/13 (Pro)", …} app.js? [sm]:105 当前环境: develop, API地址: https://dev-api.lyqf.com app.js? [sm]:155 开始检查所有插件状态... app.js? [sm]:180 vantPlugin 插件加载成功: {answer: 42, sayHello: ƒ} app.js? [sm]:205 vantPlugin 插件加载失败: TypeError: wx.getPluginVersion is not a function at Rt.checkSinglePluginStatus (app.js? [sm]:186) at Rt.<anonymous> (VM2152 WASubContext.js:1) at app.js? [sm]:162 at Array.forEach (<anonymous>) at Rt.checkAllPluginsStatus (app.js? [sm]:161) at Rt.<anonymous> (VM2152 WASubContext.js:1) at Rt.onLaunch (app.js? [sm]:71) at Rt.<anonymous> (VM2152 WASubContext.js:1) at new Rt (VM2152 WASubContext.js:1) at t.<anonymous> (VM2152 WASubContext.js:1)(env: Windows,mp,1.06.2503300; lib: 3.8.9) checkSinglePluginStatus @ app.js? [sm]:205 (anonymous) @ app.js? [sm]:162 checkAllPluginsStatus @ app.js? [sm]:161 onLaunch @ app.js? [sm]:71 (anonymous) @ app.js? [sm]:3 app.js? [sm]:180 myPlugin 插件加载成功: {getLocation: ƒ, setLocation: ƒ} app.js? [sm]:205 myPlugin 插件加载失败: TypeError: wx.getPluginVersion is not a function at Rt.checkSinglePluginStatus (app.js? [sm]:186) at Rt.<anonymous> (VM2152 WASubContext.js:1) at app.js? [sm]:162 at Array.forEach (<anonymous>) at Rt.checkAllPluginsStatus (app.js? [sm]:161) at Rt.<anonymous> (VM2152 WASubContext.js:1) at Rt.onLaunch (app.js? [sm]:71) at Rt.<anonymous> (VM2152 WASubContext.js:1) at new Rt (VM2152 WASubContext.js:1) at t.<anonymous> (VM2152 WASubContext.js:1)(env: Windows,mp,1.06.2503300; lib: 3.8.9) checkSinglePluginStatus @ app.js? [sm]:205 (anonymous) @ app.js? [sm]:162 checkAllPluginsStatus @ app.js? [sm]:161 onLaunch @ app.js? [sm]:71 (anonymous) @ app.js? [sm]:3 [Perf] App.checkAllPluginsStatus took 53ms onLaunch @ app.js? [sm]:71 (anonymous) @ app.js? [sm]:3 app.js? [sm]:254 用户未登录 [Perf] App.onLaunch took 97ms (anonymous) @ app.js? [sm]:3 wx.getSystemInfoSync is deprecated.Please use wx.getSystemSetting/wx.getAppAuthorizeSetting/wx.getDeviceInfo/wx.getWindowInfo/wx.getAppBaseInfo instead. getSystemInfoSync @ version.js? [sm]:7 gte @ version.js? [sm]:35 canIUseFormFieldButton @ version.js? [sm]:43 (anonymous) @ index.js? [sm]:7 [Deprecation] SharedArrayBuffer will require cross-origin isolation as of M92, around July 2021. See https://developer.chrome.com/blog/enabling-shared-array-buffer/ for more details. invalid app.json permission["scope.writePhotosAlbum"]
06-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值