JavaScript / ES6

ES6 destructuring an object with string keys

For properties that can't be accessed with dot notation because they contain things like hyphens or spaces

// object with hyphenated props
let person = {
  "first-name": "Jason",
  "last-name": "Watmore"

// ES6 destructure to assign props to variables using key: variable notation
let { "first-name": firstName, "last-name": lastName } = person;


Get date diff in days between two dates

Get the difference between two dates using the built in JavaScript Date and Math objects.

function daysBetween(startDate, endDate) {
    var millisecondsPerDay = 1000 * 60 * 60 * 24;
    var startDateUTC = Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate());
    var endDateUTC = Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate());

    return Math.floor((endDateUTC - startDateUTC) / millisecondsPerDay);

Sponsored by