JavaScript Tips n Tricks: Writing Decent JavaScript Code

I’ve had a hard time learning JavaScript over the years.  Mainly because I only learn it as I need to use it and most of the code I use is what I find doing a search for a single specific topic.  One of my goals last year was to improve these skills.  A new SPA project at work has helped me quite a bit but I keep looking for better information.  Someone had recommended the book “JavaScript the Good Parts” by Douglas Crockford.  I picked it up, read the intro, paged through it a few times, and promptly forgot I had purchased it.  A few days ago I was looking at Pluralsight to find something new to watch and found out that there was a new course called “JavaScript the Good Parts” out there authored by none other than Mr. Crockford himself.  I watched it and realized that it was the piece I was missing.  Between the book and the course I have finally found both pieces of the puzzle that is JavaScript.  I’ll be watching the course a few times to try to catch more of the nuances of the language that have evaded me.

ReportViewer: Display text as bold using HTML markup

I am trying to display a single word inside a larger text area as bold.  (Silly requirements.)  I have not been able to figure out if there is a way to do this within a standard express.  So far all my research has turned up is that I should try to turn markup on for that text box and use HTML markup.  Although, even that only has a limited set of supported tags

First I needed to turn markup on for the textbox.  Here are the instructions I found on this asp.net forum answer.

  1. If the Toolbox is not visible, click Toolbox on the View menu.
  2. Double-click or drag a Textbox report item to the design surface.
  3. Drag a field from your dataset into the text box. A placeholder is created for your field.
  4. Right-click the placeholder (<>), and then click Placeholder Properties.
  5. On the General tab, verify that the Value box contains an expression that evaluates to the field you dropped in step 3.
  6. Click HTML – Interpret HTML tags as styles. This causes the field to be evaluated as HTML.
  7. Click OK.

To manually change it without using the UI tools add the tag “HTML” into the xml.

markup

Finally, I rewrote the text in the textbox to be formatted with html instead of the rdlc formatting. It was a pain to do but in the end will be more flexible and maintainable.

C# Tips n Tricks: Get Decimal Places

I needed a quick way to get the number of decimal places on a Double value.  I am displaying the values differently if they have more than 2 decimal values.  Here is a quick helper that I threw together.

public static int GetDecimalCount(this double value)
{
    var str = value.ToStringOrEmpty();
    if (!str.Contains("."))
        return 0;
    return str.Substring(str.IndexOf(".") + 1).Length;
}

moment.js Tips n Tricks: Add Business Days

I needed a really quick and dirty way to add business days to a date.  We are currently using moment.js to handle our date functionality.  Here is the script I used:

    public addBusinessDays(date, daysToAdd) {
        var cnt = 0;
        var tmpDate = moment(date);
        while (cnt < daysToAdd) {
            tmpDate = tmpDate.add('days', 1);
            if (tmpDate.weekday() != moment().day("Sunday").weekday() && tmpDate.weekday() != moment().day("Saturday").weekday()) {
                cnt = cnt + 1;
            }
        }

        return tmpDate;
    }

Updated:

Here is another way to handle the same thing that works better for large numbers. Full credit for this code goes to leonardosantos, I am reposting it so that I have a copy.
https://github.com/leonardosantos/momentjs-business/blob/master/momentjs-business.js

/**
 * momentjs-business.js
 * businessDiff (mStartDate)
 * businessAdd (numberOfDays)
 */
(function () {
  var moment;
  moment = (typeof require !== "undefined" && require !== null) &&
           !require.amd ? require("moment") : this.moment;

  moment.fn.businessDiff = function (start) {
    start = moment(start);
    var end = this.clone();
    var start_offset = start.day() - 7;
    var end_offset = end.day();

    var end_sunday = end.clone().subtract('d', end_offset);
    var start_sunday = start.clone().subtract('d', start_offset);
    var weeks = end_sunday.diff(start_sunday, 'days') / 7;

    start_offset = Math.abs(start_offset);
    if(start_offset == 7)
      start_offset = 5;
    else if(start_offset == 1)
      start_offset = 0;
    else
      start_offset -= 2;


    if(end_offset == 6)
      end_offset--;

    return weeks * 5 + start_offset + end_offset;
  };

  moment.fn.businessAdd = function (days) {
    var d = this.clone().add('d', Math.floor(days / 5) * 7);
    var remaining = days % 5;
    while(remaining){
      d.add('d', 1);
      if(d.day() !== 0 && d.day() !== 6)
        remaining--;
    }
    return d;
  };

}).call(this);