js查询月份开始和结束日期

js查询月份开始和结束日期

月份开始和结束

月份开始和结束

整体不是很复杂,使用new Date()方法自带获取最后一天的时间

new Date(a,b,c),传递参数

参数a:是要获取的年份

参数b:是要获取的月份

参数c:是要获取的日期

传递日期为0时获取的传递月份的上月最后一天

时间月份是从0开始算起11结束,所以每次获取月份要加一;也就是当你传递的五月也就是六月,获取开始日期就要减一才能拿到对照日期

javascript 复制代码
// 查询月份开始结束
//开始日期
function getFirstDayOfMonth(year, month) {
  // return new Date(year, month-1, 1);
  let data = new Date(year, month - 1, 1);
  return (
    data.getFullYear() +
    "-" +
    (data.getMonth() + 1 > 9
      ? data.getMonth() + 1
      : "0" + (data.getMonth() + 1)) +
    "-" +
    (data.getDate() > 9 ? data.getDate() : "0" + data.getDate())
  );
}
//结束日期
function getLastDayOfMonth(year, month) {
  let data = new Date(year, month, 0);
  return (
    data.getFullYear() +
    "-" +
    (data.getMonth() + 1 > 9
      ? data.getMonth() + 1
      : "0" + (data.getMonth() + 1)) +
    "-" +
    (data.getDate() > 9 ? data.getDate() : "0" + data.getDate())
  );
}

function test1(){ 
	//示例
   println('2018年5月:' + getFirstDayOfMonth(2018,5) + ',' + getLastDayOfMonth(2018,5));
}
相关推荐
GDAL14 分钟前
HTML 中的 Canvas 样式设置全解
javascript
m0_5287238120 分钟前
HTML中,title和h1标签的区别是什么?
前端·html
Dark_programmer21 分钟前
html - - - - - modal弹窗出现时,页面怎么能限制滚动
前端·html
二十雨辰21 分钟前
[Java基础]网络编程
java·开发语言
ACGkaka_25 分钟前
抓包工具(三)Wireshark代理抓包Java程序的HTTPS请求
java·https·wireshark
GDAL26 分钟前
HTML Canvas clip 深入全面讲解
前端·javascript·canvas
禾苗种树27 分钟前
在 Vue 3 中使用 ECharts 制作多 Y 轴折线图时,若希望 **Y 轴颜色自动匹配折线颜色**且无需手动干预,可以通过以下步骤实现:
前端·vue.js·echarts
GISer_Jing32 分钟前
Javascript排序算法(冒泡排序、快速排序、选择排序、堆排序、插入排序、希尔排序)详解
javascript·算法·排序算法
Struggle Sheep40 分钟前
容器化部署tomcat
java·tomcat
贵州数擎科技有限公司1 小时前
使用 Three.js 实现流光特效
前端·webgl