【问题标题】:Trying to get first date of current month in html date picker试图在 html 日期选择器中获取当前月份的第一个日期
【发布时间】:2021-01-07 03:27:50
【问题描述】:

您好,我正在尝试在 HTML 日期选择器中获取当前月份的第一个日期。

From <input type="date" id="fdate" value=""/>
To<input type="date" id="tdate" value=""/>

我在 id="tdate 中得到今天的日期,如下所示,但是当我得到当前日期时,当月的开始日期无法得到。

var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var today = year + "-" + month + "-" + day;
document.getElementById("tdate").value = today;
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
document.getElementById("tdate").value = firstDay;

【问题讨论】:

  • 任何一个月的第一个日期是1 - 所以你可以这样做 - 你可以这样做 date.setDate(1)
  • @JaromandaX 我试过这样但无法在 id=fdate var firstDay = date.setDate(1); 中设置值document.getElementById('tdate').value = firstDay;
  • 您正在使用 id 'tdate' 设置输入两次!最后一个不应该是'fdate'吗?

标签: javascript html jquery


【解决方案1】:

date 输入字段必须采用 YYYY-MM-DD 格式。你的代码:

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

将返回一个字符串,例如Tue Sep 01 2020 00:00:00 GMT-0400 (Eastern Daylight Time),这不是你想要的。

您可以结合几个现有的 StackOverflow 答案来完成您想要的:

// https://stackoverflow.com/a/23593099/378779
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) 
        month = '0' + month;
    if (day.length < 2) 
        day = '0' + day;

    return [year, month, day].join('-');
}

// https://stackoverflow.com/a/13572682/378779
function getFstDayOfMonFnc() {
    var date = new Date();
    return new Date(date.getFullYear(), date.getMonth(), 1)
}

假设fdate 应该是本月的第一天,tdate 应该是今天的日期:

document.getElementById('fdate').value = formatDate( getFstDayOfMonFnc() );
document.getElementById('tdate').value = formatDate( new Date() );

【讨论】:

    【解决方案2】:

    var date = new Date();
    
    var monthStart = Date.UTC(date.getFullYear(), date.getMonth())
    monthStart = toIsoDateString(monthStart);
    console.log(monthStart);
    
    var nextMonthStart = Date.UTC(date.getFullYear(), date.getMonth() + 1);
    nextMonthStart = toIsoDateString(nextMonthStart);
    console.log(nextMonthStart);
    
    function toIsoDateString(utcDate) {
      var date = new Date(utcDate);
      return date.toISOString().split('T')[0];
    }

    【讨论】:

      【解决方案3】:

      我也做过这样的程序,但有一些变化,而不是我得到当前时间的第一个日期,一个月的第一天和一个月的最后一个日期。

      这是我使用的代码:

      <html>
         <head>
            <title>JavaScript Dates</title>
         </head>
         <body>
            <script>
               var date = new Date();
               document.write("Current Date: " + date );
               var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
               document.write("<br>"+firstDay);
               var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
               document.write("<br>"+lastDay);
            </script>
         </body>
      </html>
      

      您可以编辑此代码并尝试锻炼。

      【讨论】:

      • 我尝试了您提到的代码,但无法在 id=fdate 中获取值
      猜你喜欢
      • 2012-12-23
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 2021-10-29
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      相关资源
      最近更新 更多