[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

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.
0
mSH
Asked:
mSH
  • 2
1 Solution
 
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now