基于月份和年份获取该月的第一天和最后一天。

huangapple go评论56阅读模式
英文:

Based on month and year get the First day of the month and last day of month

问题

我是新手使用extjs。我有月份和年份。我需要根据月份和年份获取当月的第一天和最后一天。

我知道如何获取当前月份的第一天和最后一天。

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

例如,在下面的例子中,我有月份和年份,需要获取该月的第一天和最后一天。

var Month = '三月';
var Year = 2023;
英文:

I am new to extjs. i have month and year. i need 1st day of the month and last day of the month based on month and year

i know how to get 1st day and last day of the current month



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

for example below i have month and year.. need to get first and last day of the month

var Month = 'March'
var Year = 2023

答案1

得分: 0

function getFirstDayOfMonth(year, month) {
  return new Date(year, month, 1);
}

const firstDayJanuary = getFirstDayOfMonth(2023, 0);
console.log('Full Date with First Day : ' + firstDayJanuary);
console.log("First Day of January 2023 is - " + firstDayJanuary.getDay());

function getLastDayOfMonth(year, month) {
  return new Date(year, month + 1, 0);
}

const lastDayJan = getLastDayOfMonth(2023, 0);
console.log('Full Date with last Day : ' + lastDayJan);
console.log("Last Day of January 2023 is - " + lastDayJan.getDay());

Output :

Full Date with First Day : Sun Jan 01 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
First Day of January 2023 is - 0   /*0 Indicate Sunday*/
Full Date with last Day : Tue Jan 31 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
Last Day of January 2023 is - 2   /*2 Indicate Tuesday*/
英文:
function getFirstDayOfMonth(year, month) {
  return new Date(year, month, 1);
}

const firstDayJanuary = getFirstDayOfMonth(2023, 0);
console.log('Full Date with First Day : '+firstDayJanuary);
console.log("First Day of January 2023 is - "+ firstDayJanuary.getDay());


function getLastDayOfMonth(year, month) {
  return new Date(year, month + 1, 0);
}

const lastDayJan = getLastDayOfMonth(2023, 0);
console.log('Full Date with last Day : '+lastDayJan);
console.log("Last Day of January 2023 is - "+ lastDayJan.getDay());

Output :

Full Date with First Day : Sun Jan 01 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
First Day of January 2023 is - 0   /*0 Indicate Sunday*/
Full Date with last Day : Tue Jan 31 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
Last Day of January 2023 is - 2   /*2 Indicate Tuesday*/

huangapple
  • 本文由 发表于 2023年5月6日 15:59:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76187777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定