Some Date function

Hi all, please help me to calculate Date as follow:

d = new Date();

1. Calculate the yesterday date of d.
2. Calculate the last month of d
3. Cal the week of the year of d
4. Cal the quater of the year ( 3 month is a quater: 1,2,3==> quater1; 4,5,6 ==> Quater 2 etc)

Thanks a lot.
LVL 1
mSHAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

clockwatcherCommented:
Try the following:

<html>
<head>
<script language="Javascript">
<!--
function yesterday(myDate) {
  return new Date(myDate.getTime() - 86400000);
}

function lastMonth(myDate) {
  return myDate.getMonth()==0 ? 12 : myDate.getMonth();
}

function weekOfTheYear(myDate) {
  var millisPerWeek = 604800000;
  var start = new Date("Jan 1, " + myDate.getFullYear());
  return parseInt( (myDate.getTime() - start.getTime()) / millisPerWeek ) + 1;
}

function quarter(myDate) {
  return parseInt(myDate.getMonth() / 3) + 1;
}
// -->
</script>
</head>
<body>
<script language="Javascript">

//Sample usage

 var d = new Date();
 document.writeln("Now: " + d + "<BR>");
 document.writeln("Yesterday: " + yesterday(d) + "<BR>");
 document.writeln("Last Month: " + lastMonth(d) + "<BR>");
 document.writeln("Week of the Year: " + weekOfTheYear(d) + "<BR>");
 document.writeln("Quarter: " + quarter(d) + "<BR>");
</script>
</body>
</html>


I used the getFullYear method which is only available in the 4.0 browsers because of the browser inconsistencies and Y2K problems with getYear.

I calculated week of the year based on the first seven days of the year being week one, and didn't base it on any particular day defining the first day of the week (e.g, your year starts on Tuesday, but you want Sunday to be your defining day to start week counting).
0
mSHAuthor Commented:
Thanks clockwatcher! I'd want a Date obj for new value, the first week of year is start from Monday and an easer way to do that.
However you're worthy to get the point. Please give me an answer for:
1. getting the date of the first Monday of year.
2. Getting the last date of current month.

Thank you very much.

0
clockwatcherCommented:
Here's those additions you asked for:

<html>
<head>
<script language="Javascript">
<!--
function yesterDay(myDate) {
  return new Date(myDate.getTime() - 86400000);
}

function lastMonth(myDate) {
  return myDate.getMonth()==0 ? 12 : myDate.getMonth();
}

function weekOfTheYear(myDate) {
  var millisPerWeek = 604800000;
  var start = new Date("Jan 1, " + myDate.getFullYear());
  return parseInt( (myDate.getTime() - start.getTime()) / millisPerWeek ) + 1;
}

function quarter(myDate) {
  return parseInt(myDate.getMonth() / 3) + 1;
}

function getLastDayOfTheMonth(myDate) {
  var day = new Date(myDate.getFullYear(), myDate.getMonth() + 1, 1);
  return yesterDay(day);
}

function firstMonday(myDate) {
  var jan1 = new Date(myDate.getFullYear(), 0, 1);
  var day = jan1.getDay();
  switch(day) {
      case 0:
        return new Date(myDate.getFullYear(), 0 , 2);
      case 1:
        return jan1;
      default:
        return new Date(myDate.getFullYear(), 0, 9-day);
  }
}

// -->
</script>
</head>
<body>
<script language="Javascript">

var d = new Date("Dec 1, 2001");
 document.writeln("Now: " + d + "<BR>");
 document.writeln("Yesterday: " + yesterDay(d) + "<BR>");
 document.writeln("Last Month: " + lastMonth(d) + "<BR>");
 document.writeln("Week of the Year: " + weekOfTheYear(d) + "<BR>");
 document.writeln("Quarter: " + quarter(d) + "<BR>");
 document.writeln("Last Day of the Month: " + getLastDayOfTheMonth(d) + "<BR>");
 document.writeln("First Monday of the Year: " + firstMonday(d) + "<BR>");
</script>
</body>
</html>

<body>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.