js獲取當(dāng)前時(shí)間(昨天、今天、明天)
本文實(shí)例為大家分享了js獲取當(dāng)前時(shí)間的具體代碼,供大家參考,具體內(nèi)容如下
js獲取當(dāng)前時(shí)間(昨天、今天、明天)
開發(fā)過程中某些前臺(tái)頁面的時(shí)間控件我們需要給默認(rèn)當(dāng)前時(shí)間,jquery可以輕松的幫我們實(shí)現(xiàn),代碼如下
//昨天的時(shí)間 var day1 = new Date(); day1.setTime(day1.getTime()-24*60*60*1000); var s1 = day1.getFullYear()+"-" + (day1.getMonth()+1) + "-" + day1.getDate(); //今天的時(shí)間 var day2 = new Date(); day2.setTime(day2.getTime()); var s2 = day2.getFullYear()+"-" + (day2.getMonth()+1) + "-" + day2.getDate(); //明天的時(shí)間 var day3 = new Date(); day3.setTime(day3.getTime()+24*60*60*1000); var s3 = day3.getFullYear()+"-" + (day3.getMonth()+1) + "-" + day3.getDate(); //拼接時(shí)間 function show(){ var str = "" + s1 + "至" + s2; return str; } //賦值doubleDate $('#dateS').val(show());
下面是具體到時(shí)分秒的獲取方法
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();//得到小時(shí) 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í)行間隔時(shí)間,每1000毫秒刷新一次。 var timer = setTimeout("writeCurrentDate()", 1000); }
js 日期 獲取今天、昨天、明天第一個(gè)函數(shù)
function getDay(day){ var today = new Date() // 獲取時(shí)間戳(毫秒級) /* day為1,則是,明天的時(shí)間戳 day為-1,則是,昨天的時(shí)間戳 day為-2,則是,前天的時(shí)間戳 */ var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day // Date.setTime(時(shí)間戳):設(shè)置當(dāng)前日期的時(shí)間 today.setTime(targetday_milliseconds) console.log('today=', today) // today= Sun Mar 05 2023 16:14:56 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間) 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獲取今天昨天明天的日期第二個(gè)函數(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", "-"));
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Axios?get?post請求傳遞參數(shù)的實(shí)現(xiàn)代碼
axios是基于promise用于瀏覽器和node.js的http客戶端,支持瀏覽器和node.js,能攔截請求和響應(yīng),這篇文章主要介紹了axios?get?post請求傳遞參數(shù)的操作代碼,需要的朋友可以參考下2022-11-11ES6初步了解原始數(shù)據(jù)類型Symbol的用法
ES6中為我們新增了一個(gè)原始數(shù)據(jù)類型Symbol,大家是否知道Symbol可以作用在哪?用來定義對象的私有變量如何寫入對象,本文對ES6 Symbol的用法介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10js讀取json文件片段中的數(shù)據(jù)實(shí)例
下面小編就為大家?guī)硪黄猨s讀取json文件片段中的數(shù)據(jù)實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03three.js 制作動(dòng)態(tài)二維碼的示例代碼
這篇文章主要介紹了three.js 制作動(dòng)態(tài)二維碼的示例代碼,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07JavaScript極簡入門教程(二):對象和函數(shù)
這篇文章主要介紹了JavaScript極簡入門教程(二):對象和函數(shù),本文講解了對象基礎(chǔ)知識(shí)、函數(shù)基礎(chǔ)知識(shí)、函數(shù)調(diào)用、異常、繼承等內(nèi)容,需要的朋友可以參考下2014-10-10動(dòng)態(tài)創(chuàng)建script標(biāo)簽實(shí)現(xiàn)跨域資源訪問的方法介紹
本篇文章主要是對動(dòng)態(tài)創(chuàng)建script標(biāo)簽實(shí)現(xiàn)跨域資源訪問的方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02