-1

我必须将今天的日期与日历日期进行比较(使用日期选择器 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
4

2 回答 2

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');

小提琴

于 2012-07-04T13:34:53.377 回答
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");
于 2012-07-04T13:32:04.567 回答