Created
September 15, 2015 09:22
-
-
Save jdelight/3fdf07862de7e072f1bf to your computer and use it in GitHub Desktop.
ES6 Destructuring assignments with Objects & Arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// { days } creates the variable days from the default object {days: 7} | |
function get_date_range({ days } = {days: 7}) { | |
let end = Date.now(); | |
let period = days * 24 * 60 * 60 * 1000; | |
let start = end - period; | |
let start_date = new Date(start).toISOString(); // returns the date in ISO8601 format in UTC 0 | |
let end_date = new Date(end).toISOString(); | |
console.log(start_date, '-', end_date); | |
return [start_date, end_date]; | |
} | |
// overrides the default value of days | |
get_date_range({days: 4}); // 2015-09-14T09:12:10.508Z - 2015-09-15T09:12:10.508Z | |
// [ start, end ] creates the variables start & end from array returned from get_date_range() | |
let [ start, end ] = get_date_range(); // uses the default value of 7 days | |
console.log(start); // 2015-09-08T09:15:10.634Z | |
console.log(end); // 2015-09-15T09:15:22.893Z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment