Skip to content

Instantly share code, notes, and snippets.

@djleeds
Last active December 12, 2015 09:19
Show Gist options
  • Save djleeds/4751082 to your computer and use it in GitHub Desktop.
Save djleeds/4751082 to your computer and use it in GitHub Desktop.
Converting an HTML5 Date Picker value to a JavaScript Date object might not produce the result you expect. These gists demonstrate how you can create the Date object at GMT or your local time zone.
<script>
$(document).ready(function() {
$("#execute").bind("click", function() {
var dateString = $("#date").val();
var date = new Date(dateString);
$("#dateObject").text(date.toString());
});
});
</script>
<input id="date" type="date" name="date1" value="2013-02-14" />
<input id="execute" type="button" value="Create Date object" />
<span id="dateObject">(date will dump here)</span>
<script>
$(document).ready(function() {
$("#execute").bind("click", function() {
var dateString = $("#date").val().replace(/-/g, "/");
var date = new Date(dateString);
$("#dateObject").text(date.toString());
});
});
</script>
<input id="date" type="date" name="date1" value="2013-02-14" />
<input id="execute" type="button" value="Replace hyphens and create Date object" />
<span id="dateObject">(date will dump here)</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment