4

Ideally what i would like to do is take an input field attach a datepicker to it have someone pick a future date in time for example. [today is 08/09/2013 at the time of writing this and get 07/31/2013 in return.]

I want for no matter what date is picked, the input field using javascript, will always default the value to the Wednesday prior to the week in-which was the initial selected date value.

function getWednesday(date) {
    var d = (date.getDay() == 0 ? 6 : date.getDay() - 3);
    date.setTime(date.getTime() - (d * 24 * 60 * 60 * 1000));
    return date;
}

This will only return the Wednesday of the current week. My understanding of the javascript functions getDay will only return 0-6 representing sun-sat. setTime function takes a string of milliseconds since January 1, 1970 and convert to a actual date in time and getTime does the exact opposite. I'm not sure im taking the right approch to have a solid solution to the problem. Thanks in Advance for any help

4

5 回答 5

1

好的,这是我能想到的最聪明的方法:

function prevWed(inDate){
  var adder=(3-inDate.getDay());
  var d=new Date(inDate.getFullYear(), inDate.getMonth(), inDate.getDate() + adder - 7);
  return d
}

更小:

return new Date(inDate.getFullYear(), inDate.getMonth(), inDate.getDate() + (3-inDate.getDay()) - 7);

这行得通吗?

于 2013-08-09T20:31:43.797 回答
0
            var d = date.getDay();
        if (d == 0) {
            date.setTime((date.getTime() - 604800000) + 86400000 * 3);
            return date;
        }
        else if (d == 1) {
            date.setTime((date.getTime() - 604800000) + 86400000 * 2);
            return date;
        }
        else if (d == 2) {
            date.setTime((date.getTime() - 604800000) + 86400000 );
            return date;
        }
        else if (d == 4) {
            date.setTime((date.getTime() - 604800000) - 86400000 );
            return date;
        }
        else if (d == 5) {
            date.setTime((date.getTime() - 604800000) - 86400000 * 2);
            return date;
        }
        else if (d == 6) {
            date.setTime((date.getTime() - 604800000) - 86400000 * 3);
            return date;
        }
        else date.setTime(date.getTime() - 604800000);
        return date;

This actually get the solution i want a bit clunky thought...lol

于 2013-08-09T20:09:31.193 回答
0

您可以调整您的功能以包含以下内容:

function getWednesday(date) {
    var d = (date.getDay() == 0 ? 6 : date.getDay() - 3);
    date.setTime(date.getTime() - (d * 24 * 60 * 60 * 1000));
    return new Date(date.getFullYear(), date.getMonth(), date.getDate()-7);
}
alert(getWednesday(new Date()));

我认为这应该有效。

于 2013-08-09T19:50:24.940 回答
0

那应该让您获得任何给定日期和时间的前一个婚礼

现场演示

var x = new Date('8/1/2013') ;
var wed ;
for(var i=7 ; i > 0 ; i--){
    var myday = x.getTime() - (i * 24 * 60 * 60 * 1000) ;
    var d = new Date(myday) ;

    if(!!~String(d).indexOf('Wed')) wed = d  

}
 console.log(wed) ;
于 2013-08-09T20:18:09.607 回答
0
var date = new Date();
var wednesday = new Date(date.setDate(date.getDate() - ((date.getDay() == 0 ? 7 : date.getDay()) + 4)));

计算今天的天数 (5) + 从上周减去到周三的天数。

于 2013-08-09T19:59:48.043 回答