jquery實(shí)現(xiàn)獲取具體時(shí)間(年月日)后3個(gè)月+1天的年月日
實(shí)現(xiàn)代碼如下:
獲取幾個(gè)月之后的時(shí)間和幾天之后的時(shí)間:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>時(shí)間戳轉(zhuǎn)換</title>
</head>
<body>
<script src="./jquery/jquery.min.js"></script>
<script>
$(function(){
let time='2023-11-17';
let return_month_time=getLastMonthDay(time,3);
console.log('固定時(shí)間為:',time,'返回的3個(gè)月后時(shí)間為:',return_month_time)
let return_day_time=getLastDay(time,3);
console.log('固定時(shí)間為:',time,'返回的n天后時(shí)間為:',return_day_time)
// 獲取n天之后的年月日
function getLastDay(day_value,n){
var last_day=new Date(day_value);
last_day.setDate(last_day.getDate()+n);
let data_r=new Date(last_day.getTime());
let return_month=data_r.getMonth()+1;
if (return_month < 10) {
return_month = '0' + return_month;
}
let return_day=data_r.getDate();
if (return_day < 10) {
return_day = '0' + return_day;
}
return (data_r.getFullYear()+'-'+return_month+'-'+return_day);
}
// 獲取3個(gè)月零1天的年月日
function getLastMonthDay(day_value,n){
var dateArr = day_value.split('-');
let year = dateArr[0]; //獲取當(dāng)前日期的年份
let month = dateArr[1]; //獲取當(dāng)前日期的月份
let day = dateArr[2]; //獲取當(dāng)前日期的日
let new_day = new Date(year,month , 0);// 固定時(shí)間年月日
new_day = new_day.getDate(); //獲取固定日期中的月的天數(shù)
let new_year = year;
let after_month = parseInt(month) + parseInt(n); // 2 是指的是獲取幾個(gè)后的時(shí)間 3就是三個(gè)月后的
if (after_month > 12) {
new_year = parseInt(new_year) + parseInt((parseInt(after_month) / 12 == 0 ? 1 : parseInt(after_month) / 12));
after_month = parseInt(after_month) % 12;
}
let new_day2 = day;// 固定日期的日
let new_days2 = new Date(new_year, after_month, 0);
new_days2 = new_days2.getDate();// 獲取3個(gè)月后的日
console.log(new_day2,'eeee',new_days2)
// 判斷n個(gè)月之后有沒有31號,如果沒有,就拿小的日賦值給大的日
if (new_day2 > new_days2) {
new_day2 = new_days2;
}
// n個(gè)月后的年月日
let t2 = new_year + '-' + after_month + '-' + new_day2;
console.log('3個(gè)月后的年月日為',t2)
var last_day=new Date(t2);
last_day.setDate(last_day.getDate()+1);
let data_r=new Date(last_day.getTime());
let return_month=data_r.getMonth()+1;
if (return_month < 10) {
return_month = '0' + return_month;
}
let return_day=data_r.getDate();
if (return_day < 10) {
return_day = '0' + return_day;
}
return (data_r.getFullYear()+'-'+return_month+'-'+return_day);
}
})
</script>
</body>
</html>其他函數(shù)使用:
獲取JavaScript 的時(shí)間使用內(nèi)置的Date函數(shù)完成
var mydate = new Date(); mydate.getYear(); //獲取當(dāng)前年份(2位) mydate.getFullYear(); //獲取完整的年份(4位,1970-????) mydate.getMonth(); //獲取當(dāng)前月份(0-11,0代表1月) mydate.getDate(); //獲取當(dāng)前日(1-31) mydate.getDay(); //獲取當(dāng)前星期X(0-6,0代表星期天) mydate.getTime(); //獲取當(dāng)前時(shí)間(從1970.1.1開始的毫秒數(shù)) mydate.getHours(); //獲取當(dāng)前小時(shí)數(shù)(0-23) mydate.getMinutes(); //獲取當(dāng)前分鐘數(shù)(0-59) mydate.getSeconds(); //獲取當(dāng)前秒數(shù)(0-59) mydate.getMilliseconds(); //獲取當(dāng)前毫秒數(shù)(0-999) mydate.toLocaleDateString(); //獲取當(dāng)前日期 var mytime=mydate.toLocaleTimeString(); //獲取當(dāng)前時(shí)間 mydate.toLocaleString( ); //獲取日期與時(shí)間
其他使用方法如下:
jquery new date函數(shù)
在前端開發(fā)中,我們經(jīng)常需要處理日期和時(shí)間相關(guān)的操作,如計(jì)算時(shí)間差、格式化日期等。jQuery提供了一個(gè)方便的方法來操作日期和時(shí)間,即new Date()函數(shù)。本文將詳細(xì)介紹new Date()函數(shù)的用法和常見應(yīng)用場景。
語法
new Date()函數(shù)有多種重載形式,可以接受不同的參數(shù),以創(chuàng)建不同的日期對象。下面是new Date()函數(shù)的常用語法:
new Date() new Date(value) new Date(dateString) new Date(year, month, day, hours, minutes, seconds, milliseconds)
- 不傳入任何參數(shù)時(shí),new Date()將返回當(dāng)前日期和時(shí)間的對象。
- 傳入一個(gè)代表時(shí)間戳的整數(shù)參數(shù)時(shí),new Date(value)將根據(jù)該時(shí)間戳創(chuàng)建一個(gè)日期對象。
- 傳入一個(gè)代表日期的字符串參數(shù)時(shí),new Date(dateString)將根據(jù)該字符串創(chuàng)建一個(gè)日期對象。日期字符串的格式可以是ISO 8601格式(如"2022-01-01")或常見的英文日期格式(如"Jan 1, 2022")。
- 傳入年、月、日等參數(shù)時(shí),new Date(year, month, day, hours, minutes, seconds, milliseconds)將根據(jù)這些參數(shù)創(chuàng)建一個(gè)日期對象。
示例
獲取當(dāng)前日期和時(shí)間
下面的代碼示例演示了如何使用new Date()函數(shù)獲取當(dāng)前日期和時(shí)間:
let currentDate = new Date(); console.log(currentDate);
輸出結(jié)果類似于:Sat Jul 31 2022 17:45:03 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
根據(jù)時(shí)間戳創(chuàng)建日期對象
下面的代碼示例演示了如何使用new Date()函數(shù)根據(jù)時(shí)間戳創(chuàng)建日期對象:
let timestamp = 1661871600000; let date = new Date(timestamp); console.log(date);
輸出結(jié)果為:Tue Aug 30 2022 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
根據(jù)日期字符串創(chuàng)建日期對象
下面的代碼示例演示了如何使用new Date()函數(shù)根據(jù)日期字符串創(chuàng)建日期對象:
let dateString = "2022-01-01"; let date = new Date(dateString); console.log(date);
輸出結(jié)果為:Sat Jan 01 2022 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
根據(jù)年、月、日等參數(shù)創(chuàng)建日期對象
下面的代碼示例演示了如何使用new Date()函數(shù)根據(jù)年、月、日等參數(shù)創(chuàng)建日期對象:
let year = 2022; let month = 0; // 0表示一月 let day = 1; let date = new Date(year, month, day); console.log(date);
輸出結(jié)果為:Sat Jan 01 2022 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
常見應(yīng)用場景
計(jì)算時(shí)間差
new Date()函數(shù)可以與其他日期對象進(jìn)行運(yùn)算,從而實(shí)現(xiàn)時(shí)間差的計(jì)算。下面的代碼示例演示了如何計(jì)算兩個(gè)日期之間的天數(shù)差:
let startDate = new Date("2022-01-01");
let endDate = new Date("2022-12-31");
let timeDiff = endDate - startDate;
let daysDiff = Math.ceil(timeDiff / (1000 * 60 * 60 * 24));
console.log(daysDiff); // 輸出365
格式化日期
new Date()函數(shù)返回的日期對象可以通過一些方法來獲取特定的年、月、日等信息,從而實(shí)現(xiàn)日期的格式化。下面的代碼示例演示了如何將日期格式化為"YYYY-MM-DD"的形式:
let date = new Date("2022-01-01");
let year = date.getFullYear();
let month = String(date.getMonth() + 1).padStart(2, "0");
let day = String(date.getDate()).padStart(2, "0");
let formattedDate = `${year}-${month}-${day}`;
console.log(formattedDate); // 輸出"2022-01-01"new Date()函數(shù)是jQuery中處理日期和時(shí)間的重要工具之一。通過傳入不同的參數(shù),可以創(chuàng)建不同的日期對象,進(jìn)而實(shí)現(xiàn)日期的計(jì)算和格式化等操作。在實(shí)際開發(fā)中,合理運(yùn)用。
到此這篇關(guān)于jquery實(shí)現(xiàn)獲取具體時(shí)間(年月日)后3個(gè)月+1天的年月日的文章就介紹到這了,更多相關(guān)jquery獲取時(shí)間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jquery.cookie.js實(shí)現(xiàn)用戶登錄保存密碼功能的方法
這篇文章主要介紹了jquery.cookie.js實(shí)現(xiàn)用戶登錄保存密碼功能的方法,結(jié)合實(shí)例形式詳細(xì)分析了jquery.cookie.js插件操作cookie實(shí)現(xiàn)保存用戶登錄信息的相關(guān)技巧,需要的朋友可以參考下2016-04-04
jQuery Validate格式驗(yàn)證功能實(shí)例代碼(包括重名驗(yàn)證)
本文通過實(shí)例代碼給大家介紹了jQuery Validate格式驗(yàn)證功能,代碼中包括重名驗(yàn)證的方法,需要的的朋友參考下吧2017-07-07
jquery3和layui沖突導(dǎo)致使用layui.layer.full彈出全屏iframe窗口時(shí)高度152px問題
這篇文章主要介紹了解決jquery3和layui沖突導(dǎo)致使用layui.layer.full彈出全屏iframe窗口時(shí)高度152px問題,需要的朋友可以參考下2019-05-05
jQuery實(shí)現(xiàn)可兼容IE6的遮罩功能詳解
這篇文章主要介紹了jQuery實(shí)現(xiàn)可兼容IE6的遮罩功能,詳細(xì)分析了jQuery遮罩層的布局、樣式及功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09
jquery select插件異步實(shí)時(shí)搜索實(shí)例代碼
這篇文章主要介紹了jquery select插件異步實(shí)時(shí)搜索實(shí)例代碼,需要的朋友可以參考下2017-10-10

