有没有办法比较当前日期与VARCHAR格式的数据库中存储的日期?

时间:2013-03-14 07:34:29

标签: javascript jquery mysql datepicker varchar

我有一组日期,我以varchar格式存储在我的数据库中。我使用Varchar的原因是因为我使用了jquery datepicker。

我有办法将日期与当前日期进行比较。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用datejs library

您可以使用Date.parseDate.compare来解析字符串,以比较两个日期。

$.get('date.php', function(date) {
    alert(Date.compare(Date.today(), Date.parse(date));
});

答案 1 :(得分:0)

您可以使用getDate,getMonth和getFullYear来获取存储在varchar和current中的日期的日期,月份和年份,并单独进行比较。解决方案不是那么优雅,但会起作用。

var dt = new Date('2011-01-11'); //replace this hard coded date with one from database
var todaydate = new Date() //todays date

//i have added +1 in getMonth as its index start from 0. so if month is Jan then it returns 0 instead of 1. Hence i added +1 
if (dt.getDate() == todaydate.getDate() && dt.getMonth()+1 == todaydate.getMonth()+1 && dt.getFullYear() == todaydate.getFullYear())
{
    alert('match');
}
else
{
     alert('not match');
}

另请参阅此小提琴here

相关问题