js时间转换相关
2015-06-17 17:03
525 查看
1.json时间格式转换
2.js中两个时间的差
function ChangeDateFormat(jsondate) { if (!jsondate||jsondate.length < 1) {return ""; } jsondate = jsondate.replace("/Date(", "").replace(")/", ""); if (jsondate.indexOf("+") > 0) { jsondate = jsondate.substring(0, jsondate.indexOf("+")); } else if (jsondate.indexOf("-") > 0) { jsondate = jsondate.substring(0, jsondate.indexOf("-")); } var date = new Date(parseInt(jsondate, 10)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); return date.getFullYear() + "-" + month + "-" + currentDate; } //带日期 function ChangeDateTimeFormat(jsondate) { if (!jsondate || jsondate.length < 1) { return ""; } jsondate = jsondate.replace("/Date(", "").replace(")/", ""); if (jsondate.indexOf("+") > 0) { jsondate = jsondate.substring(0, jsondate.indexOf("+")); } else if (jsondate.indexOf("-") > 0) { jsondate = jsondate.substring(0, jsondate.indexOf("-")); } var date = new Date(parseInt(jsondate)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); return date.getFullYear() + "-" + month + "-" + currentDate+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds(); }
2.js中两个时间的差
function DateS(a,b,c) { var arr = ChangeDateFormat(a).split("-"); var starttime = new Date(arr[0], arr[1] - 1, arr[2]); var starttimes = starttime.getTime(); var arr = ChangeDateFormat(b).split("-"); var lktimes = new Date(arr[0], arr[1] - 1, arr[2]); var lktimes = starttime.getTime(); var ltime = 0; if (c === 'y' || c === 'Y') { ltime = (starttimes - lktimes) / (86400000*365); } if (c === 'm' || c === 'M') { ltime = (starttimes - lktimes) / (86400000 * 365/12); } if (c === 'd' || c === 'D') { ltime = (starttimes - lktimes) / 86400000; } if (c === 'h' || c === 'H') { ltime = (starttimes - lktimes) / 3600000; } if (c === 'MM' || c === 'mm') { ltime = (starttimes - lktimes) / 60000; } if (c === 'ss' || c === 'SS') { ltime = (starttimes - lktimes) / 1000; } return ltime; } //输入的值与当前时间差 function DateSNow(a, c) { var arr = ChangeDateFormat(a).split("-"); var starttime = new Date(arr[0], arr[1] - 1, arr[2]); var starttimes = starttime.getTime(); var myDate = new Date(); var mmDate = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate()); var lktimes = mmDate.getTime(); var ltime = 0; if (c === 'y' || c === 'Y') { ltime = (starttimes - lktimes) / (86400000 * 365); } if (c === 'm' || c === 'M') { ltime = (starttimes - lktimes) / (86400000 * 365 / 12); } if (c === 'd' || c === 'D') { ltime = (starttimes - lktimes) / 86400000; } if (c === 'h' || c === 'H') { ltime = (starttimes - lktimes) / 3600000; } if (c === 'MM' || c === 'mm') { ltime = (starttimes - lktimes) / 60000; } if (c === 'ss' || c === 'SS') { ltime = (starttimes - lktimes) / 1000; } return ltime; }
相关文章推荐
- javascript 数组
- 理解Javascript的动态语言特性
- John:How JavaScript Timers Work
- jstl_fmt
- jstl_fmt
- 浅谈Javascript事件模拟
- Javascript之事件流
- Unable to compile class for JSP(不能编译class)
- JSON net.sf.json(json-lib包)
- 理清javascript的相关概念 DOM和BOM
- 解决JS浮点数(小数)计算加减乘除的BUG
- fastjson使用实例
- javascript循环timeout延迟执行
- javascript中string转UTF8格式byte数组
- 读取本地图片显示在JSP页面上
- js基础学习第一天(关于DOM和BOM)一
- Javascript学习篇(一)
- 如何判断一个js对象是否一个DOM对象
- 你所不了解的javascript操作DOM的细节知识点(一)
- D3js-地图中标注地点