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.

Jan 15

How to add Google plus button to your WordPress blog

In this post, I will show you how to add Google +1 button to your WordPress blog, but my tutorial involves modifying some theme files, so it depends on the theme you use. If you use a different theme, this tutorial still should give you some ideas which theme function to modify.

My current theme is Admired Theme, so I will show you how to add Google +1 button to this theme after the blog name and after each blog post title. I will also show you how to change the WordPress default Twenty Twelve theme later.

Admired Theme

1. Log into your blog, then go to Admire Options under Appearance, then turn on Show Social Icons under Social tab, but don’t fill in anything else on the form.

AdmiredOptionSocialIcon

2. Go to Google Developer’s web site and customize your Google +1 button code snippet. https://developers.google.com/+/plugins/+1button/

3. Click Editor tab under Appearance menu, then open header-social.php file and paste the code snippet from the step 2 above after this line of code:

   1: <div class="admired-social" style="color:#ffffff;">

WordPressEditor


4. Save the file and reload your blog web site, you will see the Google +1 button in the header.

5. Now open loop-single.php file, and just add Google +1 button itself. No need to add the <script> section, because the <script> section is already added in the header-social.php file.

WordPressEditor01

6. Save the file and reload the blog web site, then click on a single post, you will see a Google +1 button underneath the blog title.

Twenty Twelve

Now let’s see how to modify Twenty Twelve theme to add Google +1 button.

1. Open the header.php file in the Twenty Twelve theme, and paste the Google +1 button code snippet in it as shown as below:

WordPressEditorHeader2012

2. Save the file and reload your blog site to see the Google +1 button after your blog site name in the header.

3. Open the single.php file and only add Google +1 button itself as shown below:

WordPressSingle2012

4. Save the file and reload your blog site. Then click a post, then you will see the Google +1 button above the blog title.

Hope this helps.