簡體   English   中英

如何在javascript中從月份和年份中獲取第一個日期和最后一個日期

[英]how to get first date and last date from month and year in javascript

任何人都可以幫助我從月份和年份中找到第一個日期和最后一個日期。 我有 2 個參數月和年,例如月 = 10 和年 = 2021

我嘗試了一些代碼,但沒有答案

var prevMonthLastDay = new Date(2021, 10, 0);
var thisMonthFirstDay = new Date(2021, 10, 1);
console.log(prevMonthLastDay);
console.log(thisMonthFirstDay);

上面的代碼給出了以下輸出:

2021-10-30T17:00:00.000Z
2021-10-31T17:00:00.000Z 

我的預期答案是第一個日期2021-10-01和最后一個日期2021-10-31

你可以試試這個方法

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

你可以按照這個。

//first and last of the current month
var current_month = "April 2017";
var arrMonth = current_month.split(" ");
var first_day = new Date(arrMonth[0] + " 1 " + arrMonth[1]);

//even though I already have the values, I'm using date functions to get year and month
//because month is zero-based
var last_day = new Date(first_day.getFullYear(), first_day.getMonth() + 1, 0, 23, 59, 59);

//use moment,js to format       
var start = moment(first_day).format("YYYY-MM-DD");
var end = moment(last_day).format("YYYY-MM-DD");

答案在以下鏈接中: 如何獲取本月的最后一天

嘗試以下操作以獲得預期的輸出:

 function GetFirstAndLastDate(year, month) { var firstDayOfMonth = new Date(year, month-1, 2); var lastDayOfMonth = new Date(year, month, 1); console.log('First Day: ' + firstDayOfMonth.toISOString().substring(0, 10)); console.log('Last Day: ' + lastDayOfMonth.toISOString().substring(0, 10)); } GetFirstAndLastDate(2021, 10);

我對您真正想要引用變量名稱的內容有些困惑,因此我將它們全部包含在內:

const currentYear = 2021;
const currentMonth = 10;

const prevMonthFirstDay = new Date(currentYear, currentMonth - 2, 1);
const prevMonthLastDay = new Date(currentYear, currentMonth - 1, 0);

const currentMonthFirstDay = new Date(currentYear, currentMonth - 1, 1);
const currentMonthLastDay = new Date(currentYear, currentMonth, 0);

const nextMonthFirstDay = new Date(currentYear, currentMonth, 1);
const nextMonthLastDay = new Date(currentYear, currentMonth + 1, 0);

console.log('prev month first day:', prevMonthFirstDay);
console.log('prev month last day:', prevMonthLastDay);
console.log('current month first day:', currentMonthFirstDay);
console.log('current month last day:', currentMonthLastDay);
console.log('next month first day:', nextMonthFirstDay);
console.log('next month last day:', nextMonthLastDay);

在我的系統上,這將給出:

prev month first day: Wed Sep 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
prev month last day: Thu Sep 30 2021 00:00:00 GMT+0700 (Western Indonesia Time)
current month first day: Fri Oct 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
current month last day: Sun Oct 31 2021 00:00:00 GMT+0700 (Western Indonesia Time)
next month first day: Mon Nov 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
next month last day: Tue Nov 30 2021 00:00:00 GMT+0700 (Western Indonesia Time)

所以現在只需要格式化:

console.log(currentMonthFirstDay.toLocaleDateString('en-GB').split('/').reverse().join('-'));

這會給你:

2021-10-01

你可以試試這種方式參考

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM