javascript - 如何检查所选日期是否是本月
问题描述
如何验证日期选择器中选择的日期是否与当前月份相同。我尝试了以下方法,但它不起作用。请帮我。谢谢
$('#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>
解决方案
- 使用 匹配月份值。
new Date().getMonth()
- 对于日间比赛
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')
});
推荐阅读
- r - 如何将 Shiny modalDialog() 宽度调整为 DT 对象以充分显示表格信息?
- java - Gradle 从 .tmpl 文件生成 java 源代码
- javascript - 将所选值 div 从一个包装器更改为所选选项 div 上的另一个包装器
- postgresql - 'type_cast_required' 是 Postgresql 中未定义的消息
- google-bigquery - REGEXP_CONTAINS : 是否有任何特定的顺序
- google-kubernetes-engine - 无法使用代理与谷歌云 sql 实例建立通信
- java - 是否可以对两个不同的类使用类级别的自定义约束注释
- java - 导致 IOException 的俄语
- ruby-on-rails - Rails 控制器中未初始化的常量错误
- javascript - vue-chartjs 未在 array.push() 上更新