将今天的日期与日历日期进行比较(接下来的七天只生成警报)

时间:2012-07-04 13:03:34

标签: javascript validation

我必须将今天的日期与日历日期(使用日期选择器ajax的选择日期)进行比较。 如果日期差异超过7天,则生成警报。

        var mydate = new Date();//Todays date
        var theyear = mydate.getFullYear();
        var themonth = mydate.getMonth() + 1;
        var thetoday = mydate.getDate();

        txtDate.value='04-Jul-2012';//date picker date in this format

2 个答案:

答案 0 :(得分:2)

var mydate = new Date(); //Todays date
txtDate.value='16-Jul-2012'; //date picker date in this format

var newDate = new Date(txtDate.value); //make date object

var difference = Math.ceil((newDate-mydate) / (60*60*24*1000)); //calculate days

if (difference > 7) alert('more than 7 days');

FIDDLE

答案 1 :(得分:1)

var datePicked = '26-Jun-2012';
var datems = Date.parse(datePicked.replace(/-/g, " "));
var dateNow = new Date();
dateNow.setHours(0,0,0,0);
var msDiff = Math.abs(Number(dateNow) - datems);
// 7 Days = 604 800 000 ms
if(msDiff > 604800000) alert("Time difference greater 7 days");