gpt4 book ai didi

javascript - 如何查看选择的日期是不是这个月

转载 作者:行者123 更新时间:2023-11-29 20:42:18 24 4
gpt4 key购买 nike

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

$('#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. 使用 匹配月份值。 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')
});

关于javascript - 如何查看选择的日期是不是这个月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55124453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com