Jan 18

Add days, months and years in JavaScript

In ASP.NET, you can easily use DateTime.AddDays(), DateTime.AddMonths(), or DateTime.AddyYears() function to get a new DateTime object, but in JavaScript, there is no such built-in function. Since it is very common you have to do some date calculation or validation in JavaScript, I decided to write my own JavaScript function to add days, months, and years to a Date object to get a new Date object. Here is the JavaScript code and it is very self-explanatory:

   1: function AddDate(oldDate, offset, offsetType) {

   2:     var year = parseInt(oldDate.getFullYear());

   3:     var month = parseInt(oldDate.getMonth());

   4:     var date = parseInt(oldDate.getDate());

   5:     var hour = parseInt(oldDate.getHours());

   6:     var newDate;

   7:     switch (offsetType) {

   8:         case "Y":

   9:         case "y":

  10:             newDate = new Date(year + offset, month, date, hour);

  11:             break;

  12:  

  13:         case "M":

  14:         case "m":

  15:             newDate = new Date(year, month + offset, date, hour);

  16:             break;

  17:  

  18:         case "D":

  19:         case "d":

  20:             newDate = new Date(year, month, date + offset, hour);

  21:             break;

  22:  

  23:         case "H":

  24:         case "h":

  25:             newDate = new Date(year, month, date, hour + offset);

  26:             break;

  27:     }

  28:     return newDate;            

  29: } 

The function AddDate() takes three parameters:

1. odlDate: A JavaScript Date object that will be worked on.

2. offset: An integer (positive or negative) that determines the number of date units that will be added to (or subtracted from) the oldDate object.

3. offsetType: The measurement unit of the offset parameter. “Y” or “y” means Year, “M” or “m” means Month, “D” or “d” means Day, “H” or “h” means Hour. Likewise, you can modify the above function to allow Minute, Second, and Millisecond.

Example:

1. To get the date that is 21 days from the current date:

   1: var date21 = AddDate(new Date(), "21", "D");

2. To get the date that is 2 years before the current date:

   1: var date2 = AddDate(new Date(), "2", "Y");

Hope this helps.

Dec 10

Add winter touch to your web site

Since it is December and I am in the northern part, I figure it is time to add a little winter touch to my web site, so I added some snow flakes flying down on my web site. Have you noticed it (well, the snow only comes in December, and keep reading to find out why)? If you want to know how to do it on your web site, here is how.

1. Go to www.dynamicdrive.com to get the snow effect JavaScript code and the snow flake picture.

2. Copy and paste the JavaScript to the <body> section of your web site, and remember to change the image source of the snow flake picture to the location where you saved it. And it is done! Now refresh your web site and you will see the snow flakes flying down on your web site.

[If you use WordPress like I do, you can add the script in your header.php file by using the Editor in your current WordPress theme.]

So far the result is very satisfactory. But I want more. I would like the snow effect only be available in December, after that the snow should stop automatically, so I added a little JavaScript check for the month before loading the snow effect. Here is the final script looks like:

   1: var d = new Date();

   2: var m = d.getMonth();

   3: if (m == 11)

   4: {

   5:     // Copy and paste the JavaScript

   6:     // you downloaded

   7: }

That is. Next month the snow effect will go away by itself, and next December, it will start again by itself and I don’t have do anything!