本文介绍了将24小时转换为12小时,具有AM& PM使用Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 将以下JSON返回值从24小时格式转换为12小时格式的最佳方式是什么?下午?日期应该保持不变 - 唯一需要格式化的时间。 2011年2月4日19:00:00 PS使用jQuery,如果这样做更容易!也更喜欢一个简单的函数/代码,而不使用Date.js。解决方案 更新2:没有秒选项 更新: AM中午更正后,测试: http://jsfiddle.net/aorcsik/xbtjE/ 我创建了这个功能: p> function formatDate(date){var d = new Date(date) var hh = d.getHours(); var m = d.getMinutes(); var s = d.getSeconds(); var dd =AM; var h = hh;如果(h> = 12){h = hh-12; dd =PM; } if(h == 0){h = 12; } m = m What is the best way to convert the following JSON returned value from a 24-hour format to 12-hour format w/ AM & PM? The date should stay the same - the time is the only thing that needs formatting.February 04, 2011 19:00:00P.S. Using jQuery if that makes it any easier! Would also prefer a simple function/code and not use Date.js. 解决方案 UPDATE 2: without seconds optionUPDATE: AM after noon corrected, tested: http://jsfiddle.net/aorcsik/xbtjE/I created this function to do this:function formatDate(date) { var d = new Date(date); var hh = d.getHours(); var m = d.getMinutes(); var s = d.getSeconds(); var dd = "AM"; var h = hh; if (h >= 12) { h = hh - 12; dd = "PM"; } if (h == 0) { h = 12; } m = m < 10 ? "0" + m : m; s = s < 10 ? "0" + s : s; /* if you want 2 digit hours: h = h<10?"0"+h:h; */ var pattern = new RegExp("0?" + hh + ":" + m + ":" + s); var replacement = h + ":" + m; /* if you want to add seconds replacement += ":"+s; */ replacement += " " + dd; return date.replace(pattern, replacement);}alert(formatDate("February 04, 2011 12:00:00")); 这篇关于将24小时转换为12小时,具有AM& PM使用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 05:35