Hướng dẫn get all day of month javascript - nhận tất cả các ngày trong tháng javascript

38

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Có cách nào để có tất cả các ngày của một tháng hoặc một năm không? Tôi đang tìm kiếm điều này để vô hiệu hóa một số ngày cụ thể trong DatePicker, tôi sẽ có một trang trong văn phòng để chọn những ngày này để vô hiệu hóa.

Vì vậy, tôi sẽ cần hiển thị tất cả các ngày trong một tháng và thêm nút "Kích hoạt hoặc Deactive" bên dưới mỗi ngày. Có cách nào để tìm những ngày này với đối tượng ngày không? Tôi đã tìm thấy liên kết này chẳng hạn: hiển thị tất cả các ngày trong một tháng nhưng tôi không thực sự hiểu nó, cộng với đó là Java, tôi đang cố gắng tìm một giải pháp trong JavaScript.

Cảm ơn sự giúp đỡ của bạn

Hướng dẫn get all day of month javascript - nhận tất cả các ngày trong tháng javascript

Joshua Wade

4.1752 Huy hiệu vàng23 Huy hiệu bạc40 Huy hiệu đồng2 gold badges23 silver badges40 bronze badges

Hỏi ngày 30 tháng 10 năm 2012 lúc 19:37Oct 30, 2012 at 19:37

1

Để có được danh sách tất cả các ngày trong một tháng, bạn có thể bắt đầu với

function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}
8 vào ngày đầu tiên của một tháng, hãy tăng ngày cho đến khi tháng thay đổi.

/**
 * @param {int} The month number, 0 based
 * @param {int} The year, not zero based, required to account for leap years
 * @return {Date[]} List with date objects for each day of the month
 */
function getDaysInMonth(month, year) {
  var date = new Date(year, month, 1);
  var days = [];
  while (date.getMonth() === month) {
    days.push(new Date(date));
    date.setDate(date.getDate() + 1);
  }
  return days;
}

Phiên bản UTC

Để đáp ứng với một số nhận xét, tôi đã tạo một phiên bản sử dụng các phương thức UTC trong trường hợp bạn muốn gọi các phương thức UTC thay vì các phương thức tiêu chuẩn trả về múi giờ cục bộ.

Tôi nghi ngờ đây là thủ phạm của các bình luận nói rằng điều này không hoạt động. Bạn thường muốn đảm bảo bạn gọi các phương thức

function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}
9 nếu bạn khởi tạo nó với
/**
 * @param {int} The month number, 0 based
 * @param {int} The year, not zero based, required to account for leap years
 * @return {Date[]} List with date objects for each day of the month
 */
function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}

function getDaysInMonth(month, year) {
  var date = new Date(year, month, 1);
  var days = [];
  while (date.getMonth() === month) {
    days.push(new Date(date));
    date.setDate(date.getDate() + 1);
  }
  return days;
}

const days2020 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const days2021 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

describe("getDaysInMonthUTC", function() {
  it("gets day counts for leap years", function() {
    const actual = days2020.map(
      (day, index) => getDaysInMonthUTC(index, 2020).length
    );
    expect(actual).toEqual(days2020);
  });

  it("gets day counts for non-leap years", function() {
    const actual = days2021.map(
      (day, index) => getDaysInMonthUTC(index, 2021).length
    );
    expect(actual).toEqual(days2021);
  });
});


describe("getDaysInMonth", function() {
  it("gets day counts for leap years", function() {
    const actual = days2020.map(
      (day, index) => getDaysInMonth(index, 2020).length
    );
    expect(actual).toEqual(days2020);
  });

  it("gets day counts for non-leap years", function() {
    const actual = days2021.map(
      (day, index) => getDaysInMonth(index, 2021).length
    );
    expect(actual).toEqual(days2021);
  });
});

// load jasmine htmlReporter
(function() {
  var env = jasmine.getEnv();
  env.addReporter(new jasmine.HtmlReporter());
  env.execute();
}());
0 và ngược lại, trừ khi bạn đang cố gắng chuyển đổi múi giờ và hiển thị sự khác biệt.

function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}

Chỉnh sửa câu trả lời này

Nếu bạn nghĩ rằng có vấn đề với kịch bản này, xin vui lòng:

  • Đầu tiên xem các bài kiểm tra đơn vị hiện có bên dưới
  • Viết một trường hợp kiểm tra chứng minh nó bị hỏng.
  • Sửa mã, đảm bảo các bài kiểm tra hiện tại vượt qua.

Bài kiểm tra đơn vị

/**
 * @param {int} The month number, 0 based
 * @param {int} The year, not zero based, required to account for leap years
 * @return {Date[]} List with date objects for each day of the month
 */
function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}

function getDaysInMonth(month, year) {
  var date = new Date(year, month, 1);
  var days = [];
  while (date.getMonth() === month) {
    days.push(new Date(date));
    date.setDate(date.getDate() + 1);
  }
  return days;
}

const days2020 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const days2021 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

describe("getDaysInMonthUTC", function() {
  it("gets day counts for leap years", function() {
    const actual = days2020.map(
      (day, index) => getDaysInMonthUTC(index, 2020).length
    );
    expect(actual).toEqual(days2020);
  });

  it("gets day counts for non-leap years", function() {
    const actual = days2021.map(
      (day, index) => getDaysInMonthUTC(index, 2021).length
    );
    expect(actual).toEqual(days2021);
  });
});


describe("getDaysInMonth", function() {
  it("gets day counts for leap years", function() {
    const actual = days2020.map(
      (day, index) => getDaysInMonth(index, 2020).length
    );
    expect(actual).toEqual(days2020);
  });

  it("gets day counts for non-leap years", function() {
    const actual = days2021.map(
      (day, index) => getDaysInMonth(index, 2021).length
    );
    expect(actual).toEqual(days2021);
  });
});

// load jasmine htmlReporter
(function() {
  var env = jasmine.getEnv();
  env.addReporter(new jasmine.HtmlReporter());
  env.execute();
}());


Đã trả lời ngày 30 tháng 10 năm 2012 lúc 20:07Oct 30, 2012 at 20:07

Ruan Mendesruan MendesRuan Mendes

87.5K30 Huy hiệu vàng146 Huy hiệu bạc209 Huy hiệu đồng30 gold badges146 silver badges209 bronze badges

7

Một lớp lót để có được tất cả các ngày làm đối tượng trong một tháng to get all days as Date object in a month

const getDaysInMonth = (month, year) => (new Array(31)).fill('').map((v,i)=>new Date(year,month-1,i+1)).filter(v=>v.getMonth()===month-1)

Đã trả lời ngày 8 tháng 7 năm 2019 lúc 11:35Jul 8, 2019 at 11:35

Hướng dẫn get all day of month javascript - nhận tất cả các ngày trong tháng javascript

Chickenschickenschickens

17.1k4 Huy hiệu vàng55 Huy hiệu bạc51 Huy hiệu Đồng4 gold badges55 silver badges51 bronze badges

1

Tôi không chắc chắn từ mô tả của bạn nếu ngày vô hiệu hóa tiêu chuẩn Dates DatePicker sẽ hoạt động với bạn, vì vậy tôi sẽ trả lời trực tiếp câu hỏi của bạn.

Bạn có thể xây dựng một loạt các ngày trong một tháng khá dễ dàng bằng cách làm điều này:

var numOfDays = new Date(2012, 10, 0).getDate(); //use 0 here and the actual month
var days = new Array();

//This will construct an array with all the elements represent the day of the week 
//(i.e. Oct 30th would be days[30-1] or days[29]) and the value would be the actual 
//day of the week (i.e. Tuesday which is representing by the number 2)
for(var i=0;i<=numOfDays;i++)
{
    days[i] = new Date(2012,9,i+1).getDay(); //use month-1 here            
}
//This will give you a number from 0 - 6 which represents (Sunday - Saturday)
alert(days[29]); 

Sử dụng mảng ngày đó, bạn có thể làm bất cứ điều gì bạn muốn với nó và cũng biết vào ngày trong tuần.

Đã trả lời ngày 30 tháng 10 năm 2012 lúc 20:00Oct 30, 2012 at 20:00

0

Sử dụng trình khởi tạo mảng es6

Bạn có thể nhận được số ngày trong một tháng được chỉ định, và sau đó tạo một mảng mới với độ dài đó và mỗi ngày là mục.

const getAllDaysInMonth = (month, year) =>
  Array.from(
    { length: new Date(year, month, 0).getDate() },
    (_, i) => new Date(year, month - 1, i + 1)
  );

Bản demo trong đoạn trích ngăn

naveen

52.2K46 Huy hiệu vàng159 Huy hiệu bạc244 Huy hiệu đồng46 gold badges159 silver badges244 bronze badges

Đã trả lời ngày 6 tháng 9 năm 2021 lúc 18:14Sep 6, 2021 at 18:14

Hướng dẫn get all day of month javascript - nhận tất cả các ngày trong tháng javascript

Kylemit ♦ KylemitKyleMit

35.3k61 Huy hiệu vàng431 Huy hiệu bạc624 Huy hiệu đồng61 gold badges431 silver badges624 bronze badges

3

Đây là một vòng lặp chạy qua mỗi tháng để xác định ngày cuối cùng của tháng đó. Ngày JavaScript chỉ mục các tháng bắt đầu từ 0 và nếu bạn đặt ngày thành 0, nó sẽ trở lại vào ngày cuối cùng của tháng trước. Tiện dụng để xác định năm bước nhảy ngày cuối cùng cho tháng hai

/**
 * @param {int} The month number, 0 based
 * @param {int} The year, not zero based, required to account for leap years
 * @return {Date[]} List with date objects for each day of the month
 */
function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}

function getDaysInMonth(month, year) {
  var date = new Date(year, month, 1);
  var days = [];
  while (date.getMonth() === month) {
    days.push(new Date(date));
    date.setDate(date.getDate() + 1);
  }
  return days;
}

const days2020 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const days2021 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

describe("getDaysInMonthUTC", function() {
  it("gets day counts for leap years", function() {
    const actual = days2020.map(
      (day, index) => getDaysInMonthUTC(index, 2020).length
    );
    expect(actual).toEqual(days2020);
  });

  it("gets day counts for non-leap years", function() {
    const actual = days2021.map(
      (day, index) => getDaysInMonthUTC(index, 2021).length
    );
    expect(actual).toEqual(days2021);
  });
});


describe("getDaysInMonth", function() {
  it("gets day counts for leap years", function() {
    const actual = days2020.map(
      (day, index) => getDaysInMonth(index, 2020).length
    );
    expect(actual).toEqual(days2020);
  });

  it("gets day counts for non-leap years", function() {
    const actual = days2021.map(
      (day, index) => getDaysInMonth(index, 2021).length
    );
    expect(actual).toEqual(days2021);
  });
});

// load jasmine htmlReporter
(function() {
  var env = jasmine.getEnv();
  env.addReporter(new jasmine.HtmlReporter());
  env.execute();
}());
1 sẽ trở lại ngày 31 tháng 12 năm 2012

/**
 * @param {int} The month number, 0 based
 * @param {int} The year, not zero based, required to account for leap years
 * @return {Date[]} List with date objects for each day of the month
 */
function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}

function getDaysInMonth(month, year) {
  var date = new Date(year, month, 1);
  var days = [];
  while (date.getMonth() === month) {
    days.push(new Date(date));
    date.setDate(date.getDate() + 1);
  }
  return days;
}

const days2020 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const days2021 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

describe("getDaysInMonthUTC", function() {
  it("gets day counts for leap years", function() {
    const actual = days2020.map(
      (day, index) => getDaysInMonthUTC(index, 2020).length
    );
    expect(actual).toEqual(days2020);
  });

  it("gets day counts for non-leap years", function() {
    const actual = days2021.map(
      (day, index) => getDaysInMonthUTC(index, 2021).length
    );
    expect(actual).toEqual(days2021);
  });
});


describe("getDaysInMonth", function() {
  it("gets day counts for leap years", function() {
    const actual = days2020.map(
      (day, index) => getDaysInMonth(index, 2020).length
    );
    expect(actual).toEqual(days2020);
  });

  it("gets day counts for non-leap years", function() {
    const actual = days2021.map(
      (day, index) => getDaysInMonth(index, 2021).length
    );
    expect(actual).toEqual(days2021);
  });
});

// load jasmine htmlReporter
(function() {
  var env = jasmine.getEnv();
  env.addReporter(new jasmine.HtmlReporter());
  env.execute();
}());
2 sẽ trở lại vào ngày 31 tháng 12 năm 2011

và tất cả những điều quan trọng để tìm ra là tháng hai với

/**
 * @param {int} The month number, 0 based
 * @param {int} The year, not zero based, required to account for leap years
 * @return {Date[]} List with date objects for each day of the month
 */
function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}

function getDaysInMonth(month, year) {
  var date = new Date(year, month, 1);
  var days = [];
  while (date.getMonth() === month) {
    days.push(new Date(date));
    date.setDate(date.getDate() + 1);
  }
  return days;
}

const days2020 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const days2021 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

describe("getDaysInMonthUTC", function() {
  it("gets day counts for leap years", function() {
    const actual = days2020.map(
      (day, index) => getDaysInMonthUTC(index, 2020).length
    );
    expect(actual).toEqual(days2020);
  });

  it("gets day counts for non-leap years", function() {
    const actual = days2021.map(
      (day, index) => getDaysInMonthUTC(index, 2021).length
    );
    expect(actual).toEqual(days2021);
  });
});


describe("getDaysInMonth", function() {
  it("gets day counts for leap years", function() {
    const actual = days2020.map(
      (day, index) => getDaysInMonth(index, 2020).length
    );
    expect(actual).toEqual(days2020);
  });

  it("gets day counts for non-leap years", function() {
    const actual = days2021.map(
      (day, index) => getDaysInMonth(index, 2021).length
    );
    expect(actual).toEqual(days2021);
  });
});

// load jasmine htmlReporter
(function() {
  var env = jasmine.getEnv();
  env.addReporter(new jasmine.HtmlReporter());
  env.execute();
}());
3 Trả lại ngày 29 tháng 2 kể từ năm nhảy năm nay

var mos=['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']

for (i = 0; i < 12; i++) {
    var lastDate = new Date(2012, i+1, 0);
    $('body').append('Last day of ' + mos[i] + ' is ' + lastDate.getDate()+'
') }

Bản demo: http://jsfiddle.net/5k8sn/1/

Đã trả lời ngày 30 tháng 10 năm 2012 lúc 20:11Oct 30, 2012 at 20:11

Charlietflarlietflcharlietfl

Phim thương hiệu vàng 170K1313 gold badges117 silver badges147 bronze badges

0

Tôi đã thực hiện chức năng mà bạn yêu cầu bằng cách sử dụng một DatePicker jQuery.

Đầu tiên, thêm tất cả các ngày được chọn trong văn phòng Back để bị vô hiệu hóa vào một mảng

// format yyyy-mm-dd
var disabledDates = [
    "2012-10-01",
    "2012-10-02",
    "2012-10-30",
    "2012-09-12"
];

Thứ hai, chỉ định DatePicker với hai chức năng

$("#datepicker").datepicker({

    // only enable date if dateEnabled returns [true]
    beforeShowDay: dateEnabled,

    // once a date has been selected call dateSelected
    onSelect: dateSelected
});

Đây là định nghĩa của các chức năng cần thiết

function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}
0

Chuyển đổi ngày thành chuỗi để so sánh với ngày trong mảng

function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}
1

Khi một ngày đã được chọn quy trình

function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}
2

Đây là một fiddle http://jsfiddle.net/kyzar/7/

Chỉ nghĩ rằng nó sẽ đáng để đề cập đến mảng.indexof là một bổ sung gần đây cho tiêu chuẩn ECMA-262 và vì vậy trong trường hợp của IE7 và IE8, nó không được hỗ trợ. Trang MDN sau đây cung cấp mã thực hiện Array.indexof cho các trình duyệt này https://developer.mozilla.org/en-us/docs/javascript/reference/global_objects/array

Đã trả lời ngày 30 tháng 10 năm 2012 lúc 23:32Oct 30, 2012 at 23:32

BrunobrunoBruno

5.7021 Huy hiệu vàng25 Huy hiệu bạc43 Huy hiệu đồng1 gold badge25 silver badges43 bronze badges

1

Tôi có cách này. Mã hóa hạnh phúc ❤ !! Lưu ý: 0 = tháng 1, 1 = tháng 2, v.v ... Ví dụ W3Schools

function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}
3

Đã trả lời ngày 19 tháng 3 năm 2021 lúc 11:30Mar 19, 2021 at 11:30

3

Tại sao bạn không nhận được tất cả các ngày trong một tháng trong mảng như thế này.

function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}
4

Sau đó đếm chúng

function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}
5

Và sau đó chia tổng số bằng Math.ceil 7 để nhận số lượng hàng trong số tuần

function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}
6

Đã trả lời ngày 18 tháng 10 năm 2021 lúc 22:05Oct 18, 2021 at 22:05

Phần này "Ngày mới (năm, tháng, 0)" sẽ nhận được ngày cuối cùng của tháng, vì vậy bạn chỉ cần lặp lại từ những ngày này tạo ra tất cả các ngày Fron 1 cho đến ngày cuối cùng.

function getDaysInMonthUTC(month, year) {
  var date = new Date(Date.UTC(year, month, 1));
  var days = [];
  while (date.getUTCMonth() === month) {
    days.push(new Date(date));
    date.setUTCDate(date.getUTCDate() + 1);
  }
  return days;
}
7

Đã trả lời ngày 26 tháng 1 lúc 23:40Jan 26 at 23:40

Hướng dẫn get all day of month javascript - nhận tất cả các ngày trong tháng javascript

Làm thế nào để bạn có được tất cả các ngày trong một tháng ở JS?

setDate (ngày. getDate () + 1); } Ngày trả về; } const bây giờ = ngày mới (); // 👇 Tất cả các ngày của bảng điều khiển tháng hiện tại ...
Tạo một đối tượng ngày mới lưu trữ ngày đầu tiên của tháng đã cho ..
Lặp lại tất cả các ngày trong tháng cụ thể ..
Đẩy mỗi đối tượng ngày vào một mảng ..

Phương pháp nào của ngày JavaScript trả về ngày trong tháng?

Sử dụng phương thức getUtCdate () Hàm này trả về ngày của đối tượng trong tháng, từ 1 đến 31. Ngày theo UTC, được trả về bởi hàm getutcdate ().getUTCDate() Method This function returns the date object's day of the month, ranging from 1 to 31. The day according to UTC, is returned by the getUTCDate() function.

Làm thế nào tôi có thể có được ngày cuối cùng của tháng trong thời điểm này?

Chúng ta có thể sử dụng phương pháp Startof để có được ngày đầu tiên của tháng hiện tại.Và chúng ta có thể sử dụng phương pháp Endof để có được ngày cuối cùng của tháng hiện tại.use the endOf method to get the last day of the current month.

Làm thế nào để bạn biết nếu một tháng có 30 hoặc 31 ngày JavaScript?

JavaScript getDate () Phương thức: Phương thức này trả về số ngày trong một tháng (từ 1 đến 31) cho ngày được xác định.Giá trị trả lại: Nó trả về một số, từ 1 đến 31, đại diện cho ngày trong tháng.getDate() Method: This method returns the number of days in a month (from 1 to 31) for the defined date. Return value: It returns a number, from 1 to 31, representing the day of the month.