Open oliver1204 opened 6 years ago
日期
new Date("month dd,yyyy hh:mm:ss"); new Date("month dd,yyyy"); new Date("yyyy-mth-dd"); new Date("yyyy/mth/dd"); new Date("yyyy,mth,dd");
new Date(yyyy-mth-dd); new Date(yyyy/mth/dd); new Date(yyyy,mth,dd); new Date(yyyy,mth,dd,hh,mm,ss); new Date(ms);
var date = new Date(yy,mm,dd,hh,mm,sss); // 返回日期对象 date.getYear(); // 获取当前年份(2位) 118 date.getFullYear(); // 获取完整的年份(4位,1970-????) 2018 date.getMonth(); // 获取当前月份(0-11,0代表1月) 7-代表八月 date.getDate(); // 获取当前日(1-31) 12 date.getHours(); // 获取当前小时数(0-23) 22 date.getMinutes(); // 获取当前分钟数(0-59) 19 date.getSeconds(); // 获取当前秒数(0-59) 35 date.getMilliseconds(); // 获取当前毫秒数(0-999) 666 date.getDay(); // 获取当前星期X(0-6,0代表星期天) 0 date.toLocaleString(); // 获取本地日期与时间 "2018/8/12 下午10:19:35" date.toLocaleDateString(); // 获取本地当前日期 "2018/8/12" date.toLocaleTimeString(); // 获取本地当前时间 "下午10:19:35" date.toString(); // 获取日期与时间 "Sun Aug 12 2018 22:19:35 GMT+0800 (中国标准时间)" date.toDateString(); // 获取当前日期 "Sun Aug 12 2018" date.toLocaleTimeString(); // 获取当前时间 "22:19:35 GMT+0800 (中国标准时间)" /*日期对象转时间戳*/ date.getTime(); // 获取当前时间(从1970.1.1开始的毫秒数) 1534083575000 date.valueOf(); // 前两者精确到毫秒 1534083575000 date.parse(); // 精确到秒 /*时间戳(13位)除以1000就可获得Unix时间戳*/
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 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() // 毫秒 }; 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; } (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
1.获取当月有多少天(即获取当月的最后一天是几号,例如获取7月有多少天)
let date = new date() new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate()
2.获取 当前日期是本年度的第几周
function weekOfYear(year, month, day){ // 每周从周日开始 var date1 = new Date(year, 0, 1); //本年的第一天 var date2 = new Date(year, month-1, day, 1); //当前日期 var dayMS = 24 * 60 * 60 * 1000; var firstDay = (7 - date1.getDay()) * dayMS; //本年度,第一周有多少天。date1.getDay()本年的第一天周几 var weekMS = 7 * dayMS; date1 = date1.getTime(); date2 = date2.getTime(); // date2-date1 本年第一天距今天的天数 return Math.ceil((date2-date1-firstDay)/weekMS) + 1; }
日期
字符类型
非字符类型
浏览器对参数的兼容性
Date对象的方法
对Date的扩展,将 Date 转化为指定格式的String
妙招