JS 苹果手机日期显示NaN问题

问题描述

  • new Date("2019-12-29 10:30:00") 在IOS下显示为NaN

原因分析

  • -的日期IOS下存在兼容问题

解决方法

  • 字符串替换
let dateStr = "2019-12-29 10:30:00";
date = dateStr .replace(/-/g, "/");

其他拓展

  • 日期格式化方法
/**
 * 日期格式化
 * @param {Object} date 
 * @param {Object} fmt
 */
function formatDate(date, fmt) {
	if (date == null || date == '' || date == 'undefined') {
		return '';
	}
	date = date + '';
	date = date.replace(/-/g, "/");
	if (date.indexOf('GMT') == -1) {
		date = date.replace(/T/g, " ");
	}
	if (date.indexOf('.') != -1) {
		date = date.substr(0, date.indexOf('.'));
	}
	date = new Date(date);
	fmt = fmt || "yyyy-MM-dd hh:mm:ss";
	var o = {
		"M+": date.getMonth() + 1, //月份   
		"d+": date.getDate(), //日   
		"h+": date.getHours(), //小时   
		"m+": date.getMinutes(), //分   
		"s+": date.getSeconds(), //秒   
		"q+": Math.floor((date.getMonth() + 3) / 3), //季度   
		"S": date.getMilliseconds() //毫秒   
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}
原文地址:https://www.cnblogs.com/KevinTseng/p/12123049.html