欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實(shí)現(xiàn)獲取年月日時(shí)間的方法總結(jié)

 更新時(shí)間:2023年08月08日 09:08:38   作者:一花一world  
這篇文章主要為大家學(xué)習(xí)介紹了JavaScript如何實(shí)現(xiàn)獲取年月日以及各種格式的時(shí)間,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以了解一下

首先介紹時(shí)間的獲取

// 獲取當(dāng)前時(shí)間
let currentTime = new Date();
// 獲取年份
let year = currentTime.getFullYear();
// 獲取月份(注意月份是從0開始計(jì)數(shù)的,所以需要加1)
let month = currentTime.getMonth() + 1;
// 獲取日期
let day = currentTime.getDate();
// 獲取小時(shí)
let hours = currentTime.getHours();
// 獲取分鐘
let minutes = currentTime.getMinutes();
// 獲取秒數(shù)
let seconds = currentTime.getSeconds();
// 獲取毫秒數(shù)
let milliseconds = currentTime.getMilliseconds();
// 格式化時(shí)間為 YYYY-MM-DD
let formattedDate = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
// 格式化時(shí)間為 HH:MM:SS
let formattedTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
// 格式化時(shí)間為 YYYY-MM-DD HH:MM:SS
let formattedDateTime = `${formattedDate} ${formattedTime}`;
// 格式化時(shí)間為 MM/DD/YYYY
let formattedDateUS = `${month.toString().padStart(2, '0')}/${day.toString().padStart(2, '0')}/${year}`;
// 格式化時(shí)間為 HH:MM AM/PM
let formattedTime12Hour = `${hours % 12 || 12}:${minutes.toString().padStart(2, '0')} ${hours < 12 ? 'AM' : 'PM'}`;
console.log('當(dāng)前時(shí)間:', currentTime);
console.log('年份:', year);
console.log('月份:', month);
console.log('日期:', day);
console.log('小時(shí):', hours);
console.log('分鐘:', minutes);
console.log('秒數(shù):', seconds);
console.log('毫秒數(shù):', milliseconds);
console.log('格式化日期:', formattedDate);
console.log('格式化時(shí)間:', formattedTime);
console.log('格式化日期時(shí)間:', formattedDateTime);
console.log('格式化日期(美國格式):', formattedDateUS);
console.log('格式化12小時(shí)制時(shí)間:', formattedTime12Hour);

獲取本月和上月的開始和結(jié)束日期

function getMonthDates() {
  let today = new Date();
  let year = today.getFullYear();
  let month = today.getMonth();
  let startOfMonth = new Date(year, month, 1);
  let endOfMonth = new Date(year, month + 1, 0);
  let startOfLastMonth = new Date(year, month - 1, 1);
  let endOfLastMonth = new Date(year, month, 0);
  return {
    startOfMonth,
    endOfMonth,
    startOfLastMonth,
    endOfLastMonth,
  };
}
// 調(diào)用函數(shù)獲取本月和上月的日期范圍
let monthDates = getMonthDates();
console.log('本月的開始日期:', monthDates.startOfMonth);
console.log('本月的結(jié)束日期:', monthDates.endOfMonth);
console.log('上月的開始日期:', monthDates.startOfLastMonth);
console.log('上月的結(jié)束日期:', monthDates.endOfLastMonth);

這個(gè)封裝的方案定義了一個(gè)名為 getMonthDates 的函數(shù),該函數(shù)根據(jù)當(dāng)前日期獲取本月和上月的開始和結(jié)束日期,并返回一個(gè)包含這些日期的對(duì)象。然后,通過調(diào)用 getMonthDates 函數(shù)來獲取日期范圍,并將結(jié)果打印輸出。

這些封裝方案可以方便地獲取本月和上月的日期范圍,可以根據(jù)需要將其集成到你的項(xiàng)目中。

要獲取本季度和上季度的開始和結(jié)束日期

可以使用以下封裝方案:

function getQuarterDates() {
  let today = new Date();
  let year = today.getFullYear();
  let month = today.getMonth();
  let quarter = Math.floor(month / 3) + 1;
  let startOfQuarter = new Date(year, (quarter - 1) * 3, 1);
  let endOfQuarter = new Date(year, quarter * 3, 0);
  let startOfLastQuarter = new Date(year, (quarter - 2) * 3, 1);
  let endOfLastQuarter = new Date(year, (quarter - 1) * 3, 0);
  return {
    startOfQuarter,
    endOfQuarter,
    startOfLastQuarter,
    endOfLastQuarter,
  };
}
// 調(diào)用函數(shù)獲取本季度和上季度的日期范圍
let quarterDates = getQuarterDates();
console.log('本季度的開始日期:', quarterDates.startOfQuarter);
console.log('本季度的結(jié)束日期:', quarterDates.endOfQuarter);
console.log('上季度的開始日期:', quarterDates.startOfLastQuarter);
console.log('上季度的結(jié)束日期:', quarterDates.endOfLastQuarter);

這個(gè)封裝的方案定義了一個(gè)名為 getQuarterDates 的函數(shù),該函數(shù)根據(jù)當(dāng)前日期獲取本季度和上季度的開始和結(jié)束日期,并返回一個(gè)包含這些日期的對(duì)象。然后,通過調(diào)用 getQuarterDates 函數(shù)來獲取日期范圍,并將結(jié)果打印輸出。

要獲取上一年的開始和結(jié)束日期,可以使用以下封裝方案:

function getLastYearDates() {
  let today = new Date();
  let year = today.getFullYear();
  let startOfYear = new Date(year - 1, 0, 1);
  let endOfYear = new Date(year - 1, 11, 31);
  return {
    startOfYear,
    endOfYear,
  };
}
// 調(diào)用函數(shù)獲取上一年的日期范圍
let lastYearDates = getLastYearDates();
console.log('上一年的開始日期:', lastYearDates.startOfYear);
console.log('上一年的結(jié)束日期:', lastYearDates.endOfYear);

這個(gè)封裝的方案定義了一個(gè)名為 getLastYearDates 的函數(shù),該函數(shù)根據(jù)當(dāng)前日期獲取上一年的開始和結(jié)束日期,并返回一個(gè)包含這些日期的對(duì)象。然后,通過調(diào)用 getLastYearDates 函數(shù)來獲取日期范圍,并將結(jié)果打印輸出。

這些封裝方案可以方便地獲取本季度、上季度以及上一年的日期范圍,可以根據(jù)需要將其集成到你的項(xiàng)目中。

整體封裝

以下是一個(gè)封裝了獲取上月、本月、本季度、上季度和年度的開始和結(jié)束時(shí)間的方法:

function getDateRange() {
  let today = new Date();
  let year = today.getFullYear();
  let month = today.getMonth();
  let quarter = Math.floor(month / 3);
  let startOfMonth = new Date(year, month, 1);
  let endOfMonth = new Date(year, month + 1, 0);
  let startOfLastMonth = new Date(year, month - 1, 1);
  let endOfLastMonth = new Date(year, month, 0);
  let startOfQuarter = new Date(year, quarter * 3, 1);
  let endOfQuarter = new Date(year, quarter * 3 + 3, 0);
  let startOfLastQuarter = new Date(year, (quarter - 1) * 3, 1);
  let endOfLastQuarter = new Date(year, quarter * 3, 0);
  let startOfYear = new Date(year, 0, 1);
  let endOfYear = new Date(year, 11, 31);
  return {
    startOfMonth,
    endOfMonth,
    startOfLastMonth,
    endOfLastMonth,
    startOfQuarter,
    endOfQuarter,
    startOfLastQuarter,
    endOfLastQuarter,
    startOfYear,
    endOfYear,
  };
}
// 調(diào)用函數(shù)獲取日期范圍
let dateRange = getDateRange();
console.log('本月的開始日期:', dateRange.startOfMonth);
console.log('本月的結(jié)束日期:', dateRange.endOfMonth);
console.log('上月的開始日期:', dateRange.startOfLastMonth);
console.log('上月的結(jié)束日期:', dateRange.endOfLastMonth);
console.log('本季度的開始日期:', dateRange.startOfQuarter);
console.log('本季度的結(jié)束日期:', dateRange.endOfQuarter);
console.log('上季度的開始日期:', dateRange.startOfLastQuarter);
console.log('上季度的結(jié)束日期:', dateRange.endOfLastQuarter);
console.log('本年度的開始日期:', dateRange.startOfYear);
console.log('本年度的結(jié)束日期:', dateRange.endOfYear);

這個(gè)封裝的方法定義了一個(gè)名為 getDateRange 的函數(shù),該函數(shù)根據(jù)當(dāng)前日期獲取上月、本月、本季度、上季度和年度的開始和結(jié)束時(shí)間,并返回一個(gè)包含這些時(shí)間的對(duì)象。然后,通過調(diào)用 getDateRange 函數(shù)來獲取日期范圍,并將結(jié)果打印輸出。

這個(gè)封裝方案可以方便地獲取上月、本月、本季度、上季度和年度的開始和結(jié)束時(shí)間,可以根據(jù)需要將其集成到你的項(xiàng)目中。

使用場(chǎng)景和優(yōu)缺點(diǎn)

上月、本月、本季度、上季度和年度的開始和結(jié)束時(shí)間可以在各種場(chǎng)景下使用,特別是與時(shí)間相關(guān)的數(shù)據(jù)分析、報(bào)表生成和業(yè)務(wù)邏輯計(jì)算等方面。以下是一些常見的使用場(chǎng)景:

數(shù)據(jù)分析和報(bào)表生成:在數(shù)據(jù)分析和報(bào)表生成過程中,經(jīng)常需要按照時(shí)間范圍進(jìn)行數(shù)據(jù)篩選和計(jì)算。使用上月、本月、本季度、上季度和年度的開始和結(jié)束時(shí)間可以方便地獲取對(duì)應(yīng)的時(shí)間范圍,從而準(zhǔn)確地提取和計(jì)算相應(yīng)的數(shù)據(jù)。

業(yè)務(wù)邏輯計(jì)算:在一些業(yè)務(wù)邏輯計(jì)算中,需要根據(jù)時(shí)間范圍進(jìn)行特定的計(jì)算和判斷。例如,統(tǒng)計(jì)上月銷售額、計(jì)算本季度的利潤等。使用上月、本月、本季度、上季度和年度的開始和結(jié)束時(shí)間可以方便地獲取相應(yīng)的時(shí)間范圍,從而進(jìn)行業(yè)務(wù)邏輯計(jì)算。

時(shí)間過濾和查詢:在數(shù)據(jù)庫查詢和數(shù)據(jù)篩選中,經(jīng)常需要根據(jù)時(shí)間范圍進(jìn)行數(shù)據(jù)過濾和查詢。使用上月、本月、本季度、上季度和年度的開始和結(jié)束時(shí)間可以方便地構(gòu)建查詢條件,從而獲取符合時(shí)間范圍要求的數(shù)據(jù)。

優(yōu)點(diǎn):

方便:使用上月、本月、本季度、上季度和年度的開始和結(jié)束時(shí)間,可以方便地獲取相應(yīng)的時(shí)間范圍,避免手動(dòng)計(jì)算和輸入時(shí)間范圍的麻煩。

精確:根據(jù)當(dāng)前日期動(dòng)態(tài)計(jì)算時(shí)間范圍,確保獲取的時(shí)間范圍準(zhǔn)確無誤。

可復(fù)用:封裝成方法后,可以在不同的場(chǎng)景中反復(fù)使用,提高代碼的復(fù)用性和效率。

缺點(diǎn):

依賴于系統(tǒng)時(shí)間:獲取的時(shí)間范圍依賴于系統(tǒng)時(shí)間,如果系統(tǒng)時(shí)間不準(zhǔn)確或被修改,可能會(huì)導(dǎo)致獲取的時(shí)間范圍不準(zhǔn)確。

邏輯復(fù)雜性:計(jì)算上月、本月、本季度、上季度和年度的開始和結(jié)束時(shí)間涉及一定的邏輯復(fù)雜性,需要考慮月份和季度的邊界情況,可能需要處理一些特殊情況的邏輯。

綜上所述,上月、本月、本季度、上季度和年度的開始和結(jié)束時(shí)間在數(shù)據(jù)分析、報(bào)表生成和業(yè)務(wù)邏輯計(jì)算等場(chǎng)景中非常有用,可以方便地獲取對(duì)應(yīng)的時(shí)間范圍。但需要注意系統(tǒng)時(shí)間的準(zhǔn)確性和處理邏輯復(fù)雜性。

到此這篇關(guān)于JavaScript實(shí)現(xiàn)獲取年月日時(shí)間的方法總結(jié)的文章就介紹到這了,更多相關(guān)JavaScript獲取時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論