Skip to content

Instantly share code, notes, and snippets.

@zwacky
Created January 17, 2016 13:22
Show Gist options
  • Save zwacky/9682e4acaaa655288305 to your computer and use it in GitHub Desktop.
Save zwacky/9682e4acaaa655288305 to your computer and use it in GitHub Desktop.
/**
* e.g. retrieving field values from a form.
*/
function getDateFromForm() {
const props = ['year', 'month', 'day', 'hour', 'minutes', 'sec', 'milisec'];
return props.map((prop) => {
return document.querySelector(`#${prop}`).text;
});
}
const dateFields = getDateFromForm();
const d = new Date(...dateFields); // et voilà
// ----------
// instead of
// ----------
var year = document.querySelector('#year').text;
var month = document.querySelector('#month').text;
var day = document.querySelector('#day').text;
var hour = document.querySelector('#hour').text;
var min = document.querySelector('#min').text;
var sec = document.querySelector('#sec').text;
var milisec = document.querySelector('#milisec').text;
var d = new Date(year, month, day, hour, min, sec, milisec);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment