JS獲取當(dāng)前時間的實(shí)例代碼(昨天、今天、明天)
js 日期 獲取今天、昨天、明天第一個函數(shù)
function getDay(day){ var today = new Date() // 獲取時間戳(毫秒級) /* day為1,則是,明天的時間戳 day為-1,則是,昨天的時間戳 day為-2,則是,前天的時間戳 */ var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day // Date.setTime(時間戳):設(shè)置當(dāng)前日期的時間 today.setTime(targetday_milliseconds) console.log('today=', today) // today= Sun Mar 05 2023 16:14:56 GMT+0800 (中國標(biāo)準(zhǔn)時間) var tYear = today.getFullYear() // 年 var tMonth = today.getMonth() // 月 var tDate = today.getDate() // 日 tMonth = this.doHandleMonth(tMonth + 1) tDate = this.doHandleMonth(tDate) console.log('返回年月日=', tYear + '-' + tMonth + '-' + tDate) return tYear + '-' + tMonth + '-' + tDate } function doHandleMonth(month) { var m = month if (month.toString().length == 1) { m = '0' + month } return m }
js獲取今天昨天明天的日期第二個函數(shù)
/* * @params date 日期 * @params type 日期 prev/current/next 昨天/今天/明天 * @params fmt 日期拼接符 */ function getDays(date, type, fmt) { let currentDate = new Date(date) let y = currentDate.getFullYear() let m = currentDate.getMonth() + 1 let d = currentDate.getDate() function dateFormat(date, fmt) { let y = new Date(date).getFullYear() let m = new Date(date).getMonth() + 1 let d = new Date(date).getDate() return `${y}${fmt}${m}${fmt}$vvxyksv9kd` } switch (type) { case "prev": if (d - 1 < 1) { if (m - 1 < 1) { y = y - 1 m = 12 } else { m = m - 1 } d = new Date(y, m, 0).getDate() } else { d = d - 1 } break case "current": break case "next": if (d + 1 > new Date(y, m, 0).getDate()) { if (m + 1 > 12) { y = y + 1 m = 1 d = 1 } else { m = m + 1 d = 1 } } else { d = d + 1 } break; default: break; } return dateFormat(new Date(`${y}-${m}-$vvxyksv9kd`), fmt) } console.log(getDays(new Date('2023-5-13'), "prev", "-")); console.log(getDays(new Date('2023-5-30'), "next", "-")); console.log(getDays(new Date('2023-5-31'), "next", "-"));
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; //當(dāng)前日期賦值給當(dāng)前日期輸入框中(jQuery easyUI) $("#currentDate").html(time); //設(shè)置得到當(dāng)前日期的函數(shù)的執(zhí)行間隔時間,每1000毫秒刷新一次。 var timer = setTimeout("writeCurrentDate()", 1000); }
3、2017.6.27更新
今天又發(fā)現(xiàn)了一種簡單的方法:可以直接對年月日時分秒進(jìn)行操作,假如今天2017-06-01 那么所得昨天為 2017-05-31
//昨天的時間 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");
其中,format函數(shù)為擴(kuò)展函數(shù)。
/** *對Date的擴(kuò)展,將 Date 轉(zhuǎn)化為指定格式的String *月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個占位符, *年(y)可以用 1-4 個占位符,毫秒(S)只能用 1 個占位符(是 1-3 位的數(shù)字) *例子: *(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; }
4、頁面日期 2017-06-27 變 20170627 “2017-06-27”.replace(/-/g,"")。、
補(bǔ)充:js獲取ISO8601規(guī)范時間
var d = new Date(); d.setHours(d.getHours(), d.getMinutes() - d.getTimezoneOffset()); console.log(d.toISOString())
總結(jié)
以上所述是小編給大家介紹的JS獲取當(dāng)前時間的實(shí)例代碼(昨天、今天、明天) ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- JS簡單獲取當(dāng)前日期和農(nóng)歷日期的方法
- JS簡單獲取當(dāng)前日期時間的方法(如:2017-03-29 11:41:10 星期四)
- 在JSP頁面中獲取當(dāng)前日期時間的方法
- Js獲取當(dāng)前日期時間及格式化代碼
- js顯示當(dāng)前日期時間和星期幾
- JS獲取當(dāng)前時間的兩種方法小結(jié)
- js 判斷當(dāng)前時間是否處于某個一個時間段內(nèi)
- 詳解js實(shí)時獲取并顯示當(dāng)前時間的方法
- JS實(shí)現(xiàn)HTML頁面中動態(tài)顯示當(dāng)前時間完整示例
- js將當(dāng)前時間格式化為 年-月-日 時:分:秒的實(shí)現(xiàn)代碼
- JS獲取當(dāng)前日期 YYYY-MM-DD hh-mm-ss的示例代碼
相關(guān)文章
JavaScript實(shí)現(xiàn)前端倒計時效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)前端倒計時效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-02-02基于JavaScript實(shí)現(xiàn)生成名片、鏈接等二維碼
本文使用javascript技術(shù)實(shí)現(xiàn)生成名片、鏈接等二維碼的代碼,代碼簡單易懂并附有注釋,需要的朋友可以參考下本文2015-09-09Javascript實(shí)現(xiàn)拖拽排序的代碼
這篇文章主要介紹了Javascript實(shí)現(xiàn)拖拽排序的代碼,本文在vue運(yùn)行環(huán)境下給大家演示下效果圖,對js拖拽排序?qū)嵗a感興趣的朋友跟隨小編一起看看吧2022-09-09使用ECharts進(jìn)行數(shù)據(jù)可視化的代碼詳解
ECharts 是一個由百度開源的強(qiáng)大、靈活的 JavaScript 圖表庫,用于在 Web 頁面上創(chuàng)建各種類型的數(shù)據(jù)可視化圖表,它具有豐富的圖表類型、強(qiáng)大的配置選項(xiàng)和良好的跨平臺兼容性,本文介紹了如何使用ECharts進(jìn)行數(shù)據(jù)可視化,需要的朋友可以參考下2024-08-08JavaScript鼠標(biāo)拖動事件監(jiān)聽使用方法以及實(shí)例效果演示
最近工作中遇到了鼠標(biāo)拖動事件監(jiān)聽的相關(guān)需求,所以下面這篇文章主要給大家介紹了關(guān)于JavaScript鼠標(biāo)拖動事件監(jiān)聽使用方法以及實(shí)例效果演示的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05