简体   繁体   English

如何检查所选日期是否是本月

[英]How to check selected date is this month or not

How to validate if the date selected in the datepicker is the same month as the current month or not.如何验证日期选择器中选择的日期是否与当前月份相同。 I tried the following but it isn't working.我尝试了以下但它不起作用。 Please help me.请帮我。 Thank you谢谢

 $('#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>

  1. Match the month value using .使用 匹配月份值。 new Date().getMonth()
  2. For day match new Date().getDate()对于日匹配new Date().getDate()

Updated http://jsfiddle.net/9y36pq85/更新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')
});

 $('#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>

You can do like this use month() function of momentjs你可以像这样使用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/ http://jsfiddle.net/viethien/47odqehb/4/

You can use the getMonth and getYear methods of the Date object, and compare the 2.您可以使用 Date 对象的getMonthgetYear方法,并比较 2。

something like就像是

$('#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');
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查到期日期是否在一个月以下? - How to check if expiration date is under a month? 检查所选日期值是否比今天的日期大至少一个月 - Check that the selected date value is at least one month greater than today's date 如何使用正确的格式从所选日期中减去 1 个月 - How to substract 1 month from selected date with the right format 如何将选定的日期月份和年份打印到不同的div标签中 - how to print selected date month and year into different div tag 相对于在另一个文本框中选择月份,如何仅在日期选择器中显示所选月份的日期? - How to display selected month dates only in date picker with respect to selecting month in another text box? 如何使用JavaScript检查给定的日期是否等于当月的月份? - How to check given date is equal to current month using javascript? 如何检查混合日期格式的月和日顺序? - How can I check month and day order in mixed date format? Javascript / jQuery:如何检查日期是否为每月的最后一天 - Javascript / jQuery: How to check if date is last day of month 如何使用moment.js将选定的日期转换为上个月和去年的日期? - How to convert the selected date to last month and last year date using moment.js? 将 90 天添加到日期选择器中选择的当前日期时如何获得正确的结束日期(月份更新不正确) - How to get correct end date when adding 90 days to current date selected in datepicker (month updates incorrectly)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM