微信小程序,IOS兼容日期格式问题,注意ios高低版本兼容性不一致

问题描述:

今天调试代码的时候出现以下问题,在部分 iOS 下无法正常使用,iOS 只支持 "yyyy/MM/dd"、"yyyy/MM/dd HH:mm:ss"、"yyyy-MM-dd"、"yyyy-MM-ddTHH:mm:ss"、"yyyy-MM-ddTHH:mm:ss+HH:mm" 的格式当时页面展示时间格式为,yyyy-mm-dd hh:mm:ss。

解决方法:

修改为ios支持的时间格式 比如 yyyy/mm/dd hh:mm:ss ,例如如下时间格式转换 格式为

{year}/{month}/{day} {hours}:{minutes}:{seconds}

javascript 复制代码
export default function formatDate(dataStr) {
  const date = new Date(dataStr);
  if (!isNaN(date)) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
  } else {
    // 无法解析日期,返回原始字符串或抛出错误,取决于需求
    return dataStr;
  }
}
相关推荐
择势9 小时前
基于声网 Agora RTM + RTC SDK 实现 iOS 语音聊天室 —— 常见问题汇总 & 解决方案手册
ios
择势10 小时前
基于声网 Agora RTM + RTC SDK 实现 iOS 语音聊天室(进阶封装)
ios
择势10 小时前
基于声网 Agora RTM + RTC SDK 实现 iOS 语音聊天室——从零到可跑的指南
ios
白玉cfc10 小时前
【iOS】底层原理:类的加载
ios·objective-c·xcode
前端小木屋11 小时前
uniapp与蓝牙设备连接详细步骤
前端·微信小程序
光电的一只菜鸡12 小时前
shell脚本开发技巧
开发语言·ios·swift
万岳科技系统开发13 小时前
私域直播系统开发从0到1:企业直播平台搭建全过程
前端·小程序·架构
2501_9160074713 小时前
iOS应用性能优化全面指南:从内存管理到工具使用
android·ios·性能优化·小程序·uni-app·iphone·webview
库奇噜啦呼14 小时前
【iOS】源码学习-类的加载
学习·ios·cocoa
ayqy贾杰14 小时前
我同事,40了,他vibe coding了个App
前端·ios·客户端