By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

Seeking help for text to date conversion

P: 21
I have a date input in yyyy-mm-dd format and I cannot convert it to a proper JavaScript date. The example below yields February 1, 2008 when it should be January 1, 2008. Can someone point out the error of my ways?

Thanks
a newbie

<script language="JavaScript" type="text/javascript">

function ConvertDate()
{
var dt_input = "2008-01-01";
var input_year = dt_input.substring(0,4);
var input_month = dt_input.substring(5,7);
var input_day = dt_input.substring(8,10);

var date_input = new Date(input_year, input_month, input_day);

alert(date_input);
}
</script>
Jan 9 '08 #1
Share this Question
Share on Google+
2 Replies


100+
P: 428
Months are 0 based, that is, date.getMonth() returns 0 for January and 1 for february.Using your method, you must subtract 1 from the months field to construct the correct date object.
Jan 9 '08 #2

P: 21
Many thanks mrhoo. It's now working fine.
Jan 9 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.