1.獲取昨天,今天,明天的時間
//昨天的時間 var day1 = new Date(); day1.setTime(day1.getTime()-24*60*60*1000); var s1 = day1.getFullYear()+"-" + (day1.getMonth()+1) + "-" + day1.getDate(); //今天的時間 var day2 = new Date(); day2.setTime(day2.getTime()); var s2 = day2.getFullYear()+"-" + (day2.getMonth()+1) + "-" + day2.getDate(); //明天的時間 var day3 = new Date(); day3.setTime(day3.getTime()+24*60*60*1000); var s3 = day3.getFullYear()+"-" + (day3.getMonth()+1) + "-" + day3.getDate(); //拼接時間 function show(){ var str = "" + s1 + "至" + s2; return str; } //賦值doubleDate $('#dateS').val(show());
2.時分秒,星期,可單個獲取
function writeCurrentDate() { var now = new Date(); var year = now.getFullYear(); //得到年份 var month = now.getMonth();//得到月份 var date = now.getDate();//得到日期 var day = now.getDay();//得到周幾 var hour = now.getHours();//得到小時 var minu = now.getMinutes();//得到分鍾 var sec = now.getSeconds();//得到秒 var MS = now.getMilliseconds();//獲取毫秒 var week; month = month + 1; if (month < 10) month = "0" + month; if (date < 10) date = "0" + date; if (hour < 10) hour = "0" + hour; if (minu < 10) minu = "0" + minu; if (sec < 10) sec = "0" + sec; if (MS < 100)MS = "0" + MS; var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"); week = arr_week[day]; var time = ""; time = year + "年" + month + "月" + date + "日" + " " + hour + ":" + minu + ":" + sec + " " + week; //當前日期賦值給當前日期輸入框中(jQuery easyUI) $("#currentDate").html(time); //設置得到當前日期的函數的執行間隔時間,每1000毫秒刷新一次。 var timer = setTimeout("writeCurrentDate()", 1000); }
我之前的一個小案例,就主要用這個寫的。小案例
3.可以直接對年月日時分秒進行操作
//昨天的時間 var day1 = new Date(); day1.setDate(day1.getDate() - 1); var s1 = day1.format("yyyy-MM-dd"); //前天的時間 var day2 = new Date(); day2.setDate(day2.getDate() - 2); var s2 = day2.format("yyyy-MM-dd");
4.format函數為擴展函數
/** *對Date的擴展,將 Date 轉化為指定格式的String *月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個占位符, *年(y)可以用 1-4 個占位符,毫秒(S)只能用 1 個占位符(是 1-3 位的數字) *例子: *(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 */ 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; }
5.頁面日期 2019-06-27 變 20190627 “2019-06-27”.replace(/-/g,"")
6.更多方法
// 獲取某個時間格式的時間戳 var date = new Date(); //時間對象 var str = date.getTime(); //轉換成時間戳 var time = new Date(“2014-07-10 10:21:12”); var str = time.getTime(); // 將時間戳換成時間格式字符串 var timestamp3 = 1403058804; //注意:要是拿到的是字符串,可以通過parseInt等方法轉成數字才可以 var newDate = new Date(); newDate.setTime(timestamp3 * 1000); // Wed Jun 18 2014 console.log(newDate.toDateString()); // Wed, 18 Jun 2014 02:33:24 GMT console.log(newDate.toGMTString()); // 2014-06-18T02:33:24.000Z console.log(newDate.toISOString()); // 2014-06-18T02:33:24.000Z console.log(newDate.toJSON()); // 2014年6月18日 console.log(newDate.toLocaleDateString()); // 2014年6月18日 上午10:33:24 console.log(newDate.toLocaleString()); // 上午10:33:24 console.log(newDate.toLocaleTimeString()); // Wed Jun 18 2014 10:33:24 GMT+0800 (中國標准時間) console.log(newDate.toString()); // 10:33:24 GMT+0800 (中國標准時間) console.log(newDate.toTimeString()); // Wed, 18 Jun 2014 02:33:24 GMT console.log(newDate.toUTCString()); 封裝的函數,可直接調用: Base.RSFormatDat = function(dt, it) { var _tTime = new Date(parseInt(dt) * 1000); if (it == 0) { var tHours = _tTime.getHours(); if (tHours < 10 && tHours >= 0) { tHours = “0” + tHours; } var tMinutes = _tTime.getMinutes(); if (tMinutes < 10 && tMinutes >= 0) { tMinutes = “0” + tMinutes; } var tSeconds = _tTime.getSeconds(); if (tSeconds < 10 && tSeconds >= 0) { tSeconds = “0” + tSeconds; } return _tTime.getFullYear() + “年” + (parseInt(_tTime.getMonth(), 10) + 1) + “月” + _tTime.getDate() + "日 " + tHours + “:” + tMinutes + “:” + tSeconds; } else { return _tTime.getFullYear() + “年” + (parseInt(_tTime.getMonth(), 10) + 1) + “月” + _tTime.getDate() + “日”; } } Base.RSFormatDat (第一個參數是要轉化的時間戳,第二個參數是轉化成決定要轉成年月日還是年月日時分秒,前者傳入1,后者傳入0) 例如 把 1510151706轉化成 x年x月x日 Base.RSFormatDat (1510151706,0)
console.log(newDate.toLocaleDateString());這個很好用
延申閱讀 神奇的 toLocaleString
時間戳在線轉換工具 其實有超過10位的時間戳,是會顯示毫秒的