Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Some Date function

Posted on 1999-07-28
3
Medium Priority
?
292 Views
Last Modified: 2008-02-26
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
Comment
Question by:mSH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 25

Expert Comment

by:clockwatcher
ID: 1290101
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
 
LVL 1

Author Comment

by:mSH
ID: 1290102
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
 
LVL 25

Accepted Solution

by:
clockwatcher earned 200 total points
ID: 1290103
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question