Get previous date based on future date using Javascript

Ideally, what I would like to do is take the input field associated with the dumper so that someone selects a future date, for example. [today is 09/09/2013 at the time of writing this letter and I am receiving in exchange on 07/31/2013.]

I want that no matter what date is selected, an input field using javascript will always set the value until Wednesday until the week in which the initial date value was selected.

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 current week's Wednesday. My understanding of javascript getDay functions will only return 0-6, representing sun-sat. The setTime function takes a string of milliseconds from January 1, 1970 and converts to the actual date in time, and getTime does the exact opposite. I am not sure that I agree to have a decisive solution to this problem. Thanks to Advance for any help.

+4
source share
5 answers

Ok, here is the smartest way I can think of:

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

even less:

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

Will this work?

+1
source

You can customize your function to enable this:

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

I think this should work.

0
source
 var date = new Date(); var wednesday = new Date(date.setDate(date.getDate() - ((date.getDay() == 0 ? 7 : date.getDay()) + 4))); 

Counts today's number (5) + the number of days to subtract from last week to get to Wednesday.

0
source
  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; 

Actually I get a solution, I want a little clumsy thought ... lol

0
source

What should happen at the previous wedding of any date and time

Live demo

 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) ; 
0
source

Source: https://habr.com/ru/post/1496229/


All Articles