Skip to content

Instantly share code, notes, and snippets.

@zachgoll
Last active October 2, 2020 13:44
Show Gist options
  • Select an option

  • Save zachgoll/1c5effea6b912af5ff5bb9778581306a to your computer and use it in GitHub Desktop.

Select an option

Save zachgoll/1c5effea6b912af5ff5bb9778581306a to your computer and use it in GitHub Desktop.
Asking user timezone
const dateValue = dateInput.value; // Notice that we are grabbing the raw value, not the date value
const userSpecifiedTimezone = "EDT"; // Assume, this was selected by the user
// The date value is now a string, so grab the pieces
const dateParts = dateValue.split("-");
const year = dateParts[0];
const month = dateParts[1];
const day = dateParts[2];
// Construct a new JS date using the individual pieces and timezone specification
const dateWithTimezone = new Date(
year + "-" + month + "-" + day + " " + userSpecifiedTimezone
);
// Use the 'localized' Date methods to get the new date values
const convertedYear = dateWithTimezone.getFullYear();
const convertedMonth = dateWithTimezone.getMonth() + 1; // Month is zero-indexed
const convertedDay = dateWithTimezone.getDate();
// Format it however you want
document.querySelector("#string-val").innerHTML =
convertedYear +
"-" +
convertedMonth +
"-" +
convertedDay +
" (year-month-day)";
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment