处理Json数据中的日期类型.如/Date(1415169703000)/格式

时间:2023-03-09 04:35:48
处理Json数据中的日期类型.如/Date(1415169703000)/格式

在asp.net mvc后台返回到视图中的json数据中想对数据进行操作,发现日期类型无法直接进行操作,需要转换为指定格式才行.在网上也搜了下方法也很多,觉得有点麻烦,最终使用正则搞定了,分享下:

var jsondate="/Date(1415169703000)/";
var formatdate=eval(jsondate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
alert(formatdate.toLocaleDateString());

有时取得的日期可能为这种类型"/Date(1415169703000+0800)/",这时代码就需要调整下了:

 var jsondate="/Date(1415169703000+8000)/";
var formatdate=eval(jsondate.replace(/\/Date\((\d+)([\+\-](\d\d)(\d\d))?\)\//gi, "new Date($1)"));
alert(formatdate.toLocaleDateString());

运行结果如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOMAAACECAIAAADtHQv5AAAEJ0lEQVR4nO3cTW7iSACG4ToNG3vnc3hp5yRegnMNr2h+LpJAsuIaMxM3kBn1BaoXxsa/CdMxCl/xPooUKFcRFL0p25ESYwEF5rvfAHARSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSoUGSrXW7pKJMcYYM0l23dFw3h6rj5TmYf/4Lpmch3uXD79ma/k8NKXGG70PlGrnYZnJLplUCVSjuyRsjs3DblW7JEySnnE7D89R9S4ffs328uaTu0Opdecqa+nUUm0fagz1jDc21OHlA4Pt5ZSKk1ov9XQ6u2CrqlNB3drajfcvHxzs/ohUZ/+BawWXUWppHprGmfbSUqueegruv6C9sNShKwLbv1e7jlKtLW5qJkOn+E/O/rWdrnGn07uhdpcPDQ4t/+BF3EapA3ft86S6j0qayQzf/Vx0UXlZqd3lzdu9+7tipdTGnlhrthpv3Lz3zbTn32iVc3tPz73LewcHzu49b+l+UOo1fHHPu8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