Link to home
Start Free TrialLog in
Avatar of Matt Pinkston
Matt Pinkston

asked on

Javascript to set Date Variables

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?
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

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

Avatar of Matt Pinkston
Matt Pinkston

ASKER

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();
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
how do I add days to get another date?
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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