subarrays to array javascript code example

Example 1: new date javascript

// Several ways to create a Date object
let today = new Date();
let birthday = new Date("December 17, 1995 03:24:00");
let birthday = new Date("1995-12-17T03:24:00");
let birthday = new Date(1995, 11, 17); // the month is 0-indexed
let birthday = new Date(1995, 11, 17, 3, 24, 0);
let birthday = new Date(628021800000); // passing epoch timestamp

// To get Date, Month and Year or Time
let [month, date, year] = new Date().toLocaleDateString("en-US").split("/");
let [hour, minute, second] = new Date()
   .toLocaleTimeString("en-US")
   .split(/:| /);

// Two digit years map to 1900 – 1999
let date = new Date(98, 1); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)
// Deprecated method; 98 maps to 1998 here as well
date.setYear(98); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)
date.setFullYear(98); // Sat Feb 01 0098 00:00:00 GMT+0000 (BST)

Example 2: javascript how to get subarray

const ar  = [1, 2, 3, 4, 5];

// slice from 1..3 - add 1 as the end index is not included

const ar2 = ar.slice(1, 3 + 1);

console.log(ar2);

Example 3: return last two values of array in javascript

var arr1 = [0, 1, 2, 3, 4, 5, 6, 7],
  arr2 = [0, 1, 2, 3];

document.body.innerHTML = 'ARRAY 1: ' + arr1.slice(1).slice(-5) + '<br/>ARRAY 2: ' + arr2.slice(1).slice(-5);