【问题标题】:How to check selected date is this month or not如何检查所选日期是否是本月
【发布时间】:2019-03-12 14:53:27
【问题描述】:

如何验证在日期选择器中选择的日期是否与当前月份相同。我尝试了以下方法,但它不起作用。请帮我。谢谢

$('#thedate').datepicker({
  minDate: 0
});

$('#checkDate').bind('click', function() {
  var selectedDate = $('#thedate').datepicker('getDate');
  var today = new Date();
  today.setHours(0);
  today.setMinutes(0);
  today.setSeconds(0);
  if (Date.parse(today) == Date.parse(selectedDate)) {
    alert('This month');
  } else {
    alert('Not this month');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

Date: <input type="text" id="thedate">
<button id="checkDate">Check this month or not</button>

【问题讨论】:

    标签: javascript jquery datepicker jquery-ui-datepicker


    【解决方案1】:
    1. 使用 匹配月份值。 new Date().getMonth()
    2. 白天比赛new Date().getDate()

    更新 http://jsfiddle.net/9y36pq85/

       $('#thedate').datepicker({minDate:0});
    
    $('#checkDate').bind('click', function() {
        var selectedDate = $('#thedate').datepicker('getDate');
        var d= new Date(selectedDate);
        var today = new Date();
        if (d.getMonth() == today.getMonth()) {
            alert('this month');
        } else {
            alert('Not this  month');
        }
        alert(d.getDate() == today.getDate() ?'today':'not today')
    });
    

    【讨论】:

    • 今天怎么查?
    • 我做到了..工作正常..谢谢
    【解决方案2】:

    $('#thedate').datepicker({minDate:0});
    
    $('#checkDate').bind('click', function() {
        var selectedDate = $('#thedate').datepicker('getDate');
        
        var current = moment(selectedDate);
        if (moment().month()== current.month()) {
            alert('this month');
        } else {
            alert('Not this  month');
        }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      
    Date: <input type="text" id="thedate"/>
    
    <button id="checkDate">Check this month or not</button>

    你可以像这样使用momentjs的month()函数

    $('#thedate').datepicker({minDate:0});
    
    $('#checkDate').bind('click', function() {
        var selectedDate = $('#thedate').datepicker('getDate');
    
        var current = moment(selectedDate);
        if (moment().month()== current.month()) {
            alert('this month');
        } else {
            alert('Not this  month');
        }
    });
    

    http://jsfiddle.net/viethien/47odqehb/4/

    【讨论】:

    • 应避免使用不必要的库。
    【解决方案3】:

    您可以使用Date对象的getMonthgetYear方法,并比较两者。

    类似

    $('#checkDate').bind('click', function() {
        var selectedDate = $('#thedate').datepicker('getDate');
        var today = new Date();
        if (today.getYear() === selectedDate.getYear() && today.getMonth() === selectedDate.getMonth()) {
            alert('this month');
        } else {
            alert('Not this  month');
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-01
      • 2021-10-25
      • 2015-12-19
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多