# Javascript – Calculate the number of days between two dates

```// Checkin date
var checkin = '2013-04-04';
var checkin_date = new Date();
checkin_date.setYear(parseInt(checkin.substr(0, 4), 10));
checkin_date.setMonth(parseInt(checkin.substr(5, 2), 10) - 1);
checkin_date.setDate(parseInt(checkin.substr(8, 2), 10));

// Checkout date
var checkout = '2013-04-10';
var checkout_date = new Date();
checkout_date.setYear(parseInt(checkout.substr(0, 4), 10));
checkout_date.setMonth(parseInt(checkout.substr(5, 2), 10) - 1);
checkout_date.setDate(parseInt(checkout.substr(8, 2), 10));

// Calculate the difference
var days = (checkout_date - checkin_date)/1000/60/60/24;

// Output the result
console.log(Math.floor(days));
```

Done =)

# Javascript – Create a date object from a date string

Previously, we talked about how to convert a date object into string.
Javascript – Get YYYY-MM-DD string from a Date object

This time, we will do the opposite which parses a date string and create a date object.

```// Assume we have the date string in YYYY-MM-DD formate
var datestring = '2013-05-03';

var date_obj = new Date();
date_obj.setYear(parseInt(datestring.substr(0, 4), 10));
date_obj.setMonth(parseInt(datestring.substr(5, 2), 10) - 1);
date_obj.setDate(parseInt(datestring.substr(8, 2), 10));

// Verify the date object
console.log(date_obj);
```

Done =)

# Javascript – Break forEach loop

One way to break a forEach loop in Javascript is to make use of the try and catch.

```var BreakException= {};

try {
[1,2,3,4,5].forEach(function(val) {
if (val == 3) {
throw BreakException;
} else {
console.log(val);
}
});
} catch(e) {
if (e !== BreakException) {
throw e;
}
}

// Result: only print 1 and 2.
```

# Javascript – Get YYYY-MM-DD string from a Date object

The following piece of code shows you how to get the YYYY-MM-DD formatted string from a Javascript Date Object.

```// GET CURRENT DATE
var date = new Date();

// GET YYYY, MM AND DD FROM THE DATE OBJECT
var yyyy = date.getFullYear().toString();
var mm = (date.getMonth()+1).toString();
var dd  = date.getDate().toString();

// CONVERT mm AND dd INTO chars
var mmChars = mm.split('');
var ddChars = dd.split('');

// CONCAT THE STRINGS IN YYYY-MM-DD FORMAT
var datestring = yyyy + '-' + (mmChars[1]?mm:"0"+mmChars[0]) + '-' + (ddChars[1]?dd:"0"+ddChars[0]);
```

Done =)

# Javascript – How to Shuffle an array

You can use the following function to shuffle a Javascript array. Kudos to Ashley Pond V. The author applies the Fisher-Yates shuffle algorithm which is also known as the Knuth shuffle in Javascript.

```function fisherYates(myArray) {
var i = myArray.length;
if (i == 0) return false;
while(--i) {
var j = Math.floor(Math.random()*(i+1));
var tempi = myArray[i];
var tempj = myArray[j];
myArray[i] = tempj;
myArray[j] = tempi;
}
}
```

Done =)

# CSS Browser Selector – Align the CSS styles for different browsers and OS

We have Modernizr to handle the HTML5 and CSS3 problem in old browsers.
Modernizr – Tackle the HTML5 and CSS3 problems in old browsers for frontend programming

Today, i would like to introduce another tool which could help you aligning the CSS styles for different browsers as well as OS. This tool is called CSS Browser Selector. Although it is not updated for more than a year, it is still very useful if you need to deal with Internet Explorer 7 & 8.

Similar to Modernizr, what u need to do is just included the library in the <head> tag.

# Modernizr – Tackle the HTML5 and CSS3 problems in old browsers for frontend programming

Having problem on HTML5 and CSS3 with Internet Explorer? The Modernizr library could help you to tackle the problem. It checks the HTML5 and CSS3 features supported by the browser and developers could determine them by checking the classes of the <html> tag.

# Javascript – Default values for function input parameters

A good example says it all.

```function foo(a, b) {
a = typeof a !== 'undefined' ? a : 42;
b = typeof b !== 'undefined' ? b : 'default_b';

// Do whatever you want
}
```

Done =)