Danny
Danny

Reputation: 7518

Using new Date on value of an <input type="date" />

I am trying to convert the value of an <input type="date" /> to an actual Javascript Date object. The example is below

new Date($('#myInput').val()); //where value of myInput is '2011-01-01'

turns out to be

Fri Dec 31 2010 19:00:00 GMT-0500 (Eastern Standard Time)

It appears that the Date is convert from UTC to EST (I think). Is there anyway to prevent this conversion????

example: jsfiddle

Upvotes: 4

Views: 1453

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324760

Try this:

ver date = new Date(document.getElementById('myinput').value);
date.setTime(date.getTime()+date.getTimezoneOffset()*60000);

Alternatively, use the getUTC*() functions.

Upvotes: 0

danronmoon
danronmoon

Reputation: 3873

Use Date.prototype.toUTCString to convert your locale-specific date to UTC

(new Date($('#myInput').val())).toUTCString()

Upvotes: 2

Related Questions