JQuery 日期转换日期方法封装
1. C#返回日期格式转换为时间戳,再转化为前端日期格式; nowDate是C#返回的日期格式变量;
var timeStamp = parseInt(nowDate.replace("/Date(", "").replace(")/", ""), 10); var newTime = new Date(timeStamp); //就得到普通的时间了
2. JQuery 日期格式转换封装方法;主要有
2.1 日期格式字符串转换扩展方法:
String.prototype.formatDate = function (fmt) { if (this.valueOf() == "null" || this.valueOf() == "undefined" || this.valueOf() == "") return ""; else if (typeof this.valueOf() == "string") { if (this.valueOf().indexOf("/Date(") > -1) { var formats = typeof fmt == undefined || fmt == null || fmt == "" ? 'yyyy-MM-dd hh:mm' : fmt; var time = eval(this.valueOf().replace(/\/Date\((\d+)\)\//gi, "new Date($1)")).format(formats); return time; } } return this.valueOf(); };
2.2 日期格式转换扩展方法:
Date.prototype.format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; fmt = fmt || "yyyy-MM-dd"; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.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; }
2.3 计算两个日期的时间差值:
/** * 计算时间戳之间的差值 * @param startTime 开始时间戳 * @param endTime 结束时间戳 * @param type 返回指定类型差值(year, month, day, hour, minute, second) */ function DateDiff(startTime, endTime, type) { var timeDiff = endTime - startTime switch (type) { case "year": return Math.floor(timeDiff / 86400 / 365); break; case "month": return Math.floor(timeDiff / 86400 / 30); break; case "day": return Math.floor(timeDiff / 86400); break; case "hour": return Math.floor(timeDiff / 3600); break; case "minute": return Math.floor(timeDiff / 60); break; case "second": return timeDiff % 60; break; }
2.4 日期格式转换时间戳方法
<script type="text/javascript"> $.extend({ TimeExtension:{ CurTime: function(){ return Date.parse(new Date())/1000; }, DateToUnix: function(string) { var f = string.split(' ', 2); var d = (f[0] ? f[0] : '').split('-', 3); var t = (f[1] ? f[1] : '').split(':', 3); return (new Date( parseInt(d[0], 10) || null, (parseInt(d[1], 10) || 1) - 1, parseInt(d[2], 10) || null, parseInt(t[0], 10) || null, parseInt(t[1], 10) || null, parseInt(t[2], 10) || null )).getTime() / 1000; }, UnixToDate: function(unixTime, isFull, timeZone) { if (typeof (timeZone) == 'number'){ unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60; } var time = new Date(unixTime * 1000); var dateStr = ""; dateStr += time.getUTCFullYear() + "-"; dateStr += (time.getUTCMonth()+1) + "-"; dateStr += time.getUTCDate(); if (isFull === true){ dateStr += "" + time.getUTCHours() + ":"; dateStr += time.getUTCMinutes() + ":"; dateStr += time.getUTCSeconds(); } return dateStr; } } }); </script>
测试示例:
console.log($.TimeExtension.CurTime());
//1631515207 $.TimeExtension.UnixToDate(1631515207) //"2021-9-13" $.TimeExtension.DateToUnix("2021-9-13") //1631462400