Javascript 日期对象与隐藏值的比较
我一直在痴迷于阅读如何在 Javascript 中执行日期对象比较。有一些非常好的想法和指示,但似乎无法获得任何快乐。
这就是问题所在。我正在使用 jquery datepicker 设置隐藏值。如果隐藏值发生变化,我想将此日期与今天的日期进行比较,如果它们匹配,我想向控制台发送警报。
这是我的脚本。 chrome控制台没有报错;它似乎只是不想进行我所追求的日期比较。
var date2 = new Date();
$("select[name=sanctionDateStart_hidden]").change(function () {
if ($("select[name=sanctionDateStart_hidden]").getTime() == date2.getTime())
alert("yayayayaya");
});
感谢任何人愿意分享的任何见解。
编辑: 这就是最终解决我的问题的方法,只是发布此内容以防其他人可以使用它。感谢下面的帖子帮助我朝着正确的方向前进。
var date2 = new Date();
$("#sanctionDateStart").datepicker({
altField: "#sanctionDateStart_hidden",
altFormat: "yy-mm-dd",
minDate: new Date()
}).datepicker("setDate", "{$data.sanctionDateStart}").change(function () {
$('#sanctionDateEnd').datepicker('option', 'minDate', $(this).datepicker('getDate'));
if ($("#sanctionDateStart").datepicker("getDate").getFullYear() == date2.getFullYear()
&& $("#sanctionDateStart").datepicker("getDate").getMonth() == date2.getMonth()
&& $("#sanctionDateStart").datepicker("getDate").getDate() == date2.getDate())
{
alert ('Todays Date');
}
});
I have been reading obsessively in SO how to perform a date object comparison in Javascript. Got some really good ideas and pointers, but just cant seem to get any joy.
Here is the issue. I am using jquery datepicker to set a hidden value. If the hidden value changes, I would like to compare this date to today's date, If they match, I would like to send an alert to the console.
Here is my script. No errors are reported in the chrome console; it just doesn't seem to want to make that date comparison I am after.
var date2 = new Date();
$("select[name=sanctionDateStart_hidden]").change(function () {
if ($("select[name=sanctionDateStart_hidden]").getTime() == date2.getTime())
alert("yayayayaya");
});
Appreciate any insight anyone cares to share.
EDIT:
Here is what ended up resolving my issue, just posting this in case anyone else can use this. Thanks to post below to help me get going in the right direction.
var date2 = new Date();
$("#sanctionDateStart").datepicker({
altField: "#sanctionDateStart_hidden",
altFormat: "yy-mm-dd",
minDate: new Date()
}).datepicker("setDate", "{$data.sanctionDateStart}").change(function () {
$('#sanctionDateEnd').datepicker('option', 'minDate', $(this).datepicker('getDate'));
if ($("#sanctionDateStart").datepicker("getDate").getFullYear() == date2.getFullYear()
&& $("#sanctionDateStart").datepicker("getDate").getMonth() == date2.getMonth()
&& $("#sanctionDateStart").datepicker("getDate").getDate() == date2.getDate())
{
alert ('Todays Date');
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Javascript 日期 实际上是一个时间,包括当前日期以及小时、分钟、秒和毫秒。因此,它很少与日期选择器选择的日期相匹配
在比较中使用
onlyDateWithoutTime
。或者做
Javascript Date is actually a time that includes the current date and also hour , minutes , seconds and milliseconds. So it would seldom match a date picked by a date picker
Use
onlyDateWithoutTime
in your comparisons.Or do