微信小程序,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;
  }
}
相关推荐
世界尽头与你1 小时前
iOS 越狱检测原理
安全·网络安全·ios·信息安全·渗透测试
AI品信智慧数智人1 小时前
从键盘打字到语音对话✨数字人交互重塑小程序用户体验新范式
小程序
爱勇宝16 小时前
上线 20 多天后,我才发现小程序的图片上传一直不可用
微信小程序·智能小程序
2601_9567436818 小时前
上海小程序开发公司:本地优质厂商名录与源码私有化技术分析
小程序·开发经验·上海
_瑞1 天前
AI Coding 那么快,为什么还需要 SDD?
人工智能·ios·ai编程
科技小刘c1 天前
接单做小程序,我算了笔省下的时间账
小程序
轩辕十四leo1 天前
Astrolabe(星盘):让 AI 看见自己写出的 UI
ios·ai编程
白玉cfc1 天前
熟悉Objective-C
开发语言·ios·objective-c
星辰即远方1 天前
字符串合法性检验
macos·ios·cocoa
末代iOS程序员华仔2 天前
KMP全栈开发:从Android到AI Agent的技术演进与实践
flutter·ios·figma