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

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 に答える