Javascript to set Date Variables

Matt Pinkston
Matt Pinkston used Ask the Experts™
on
I need a javascript that will provide me some date fields based on mondays.
 
I have a sharepoint custom form that I need to display dates on in the form
mm/dd/yy

Logic
 
if dow is Monday through Thursday
date1 = current monday
date2 = following monday
date3 = monday 2 weeks out

if dow is Friday through Sunday
date1 = next monday
date2 = 2 mondays out
date3 = 3 mondays out

once I have these in a javascript variable I should be able to produce on form?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you can check the current day of the week by using getDay() method
http://www.w3schools.com/jsref/jsref_getday.asp

you can then add the days into current date

i have attached a sample program to get the current/next monday from any day of the week, you can build on that
<html>
<body>

<script type="text/javascript">
var date  =new Date();


var day = date.getDay();
switch( day )
{
   case 0:
     date  = date + 1; //next monday
   break;
   case 5:
     date  = date + 3; //next monday
   break;
   case 6:
     date  = date + 2; //next monday
   break;
   
   case 1:
     date  = date + 1; //current monday
   break;
   case 2:
     date  = date - 1; //current monday
   break;
   case 3:
     date  = date - 2; //current monday
   break;
   case 4:
     date  = date - 3; //current monday
   break;
}

document.write(date  );

</script>

</body>
</html>

Open in new window

Matt PinkstonEnterprise Architect

Author

Commented:
so this script based on dow sets date to = monday's date?
it will simply return you the monday date.

you can always reproduce the current date again, using
var newDate = new Date();
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Matt PinkstonEnterprise Architect

Author

Commented:
so case 0 is SUnday? and so on down the line?

   case 0:
     date  = date + 1; //next monday
   break;

On Sunday add 1 to get Mondays date.
<<so case 0 is SUnday? and so on down the line?>>
yes
Matt PinkstonEnterprise Architect

Author

Commented:
how do I add days to get another date?
simply adding the integer into a date object, see my attached source code carefully!!
Matt PinkstonEnterprise Architect

Author

Commented:
this does not seem to work?


<script type="text/javascript">
var date  =new Date();


var day = date.getDay();
switch( day )
{
   case 0:
     date  = date + 1; //next monday
   break;
   case 5:
     date  = date + 3; //next monday
   break;
   case 6:
     date  = date + 2; //next monday
   break;
   
   case 1:
     date  = date + 1; //current monday
   break;
   case 2:
     date  = date - 1; //current monday
   break;
   case 3:
     date  = date - 2; //current monday
   break;
   case 4:
     date  = date - 3; //current monday
   break;
}
   date1 = date +7;
   date2 = date +14;

document.write(date  );

document.write(date1  );

document.write(date2  );

</script>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial