1

I have a text box that is populated from a datepicker in Javascript. The date is stored as 30-Jan-2013. I want to convert this to a date so I can use it in other calculations.

I have tried

var date1 = new Date(document.getElementById('textbox').value)

but this returns Nan

if I drop the new Date part e.g.

var date1 = (document.getElementById('textbox').value

I get the date 30-Jan-2013 I just don't seem to be able to convert this?

12
  • 1
    Are you trying to parse the date using JavaScript or ASP.NET? Commented Jan 30, 2013 at 22:03
  • What browser are you using? In Chrome new Date('30-Jan-2013') works for me. Regardless, the format needs to be one supported by Date.parse, as documented in developer.mozilla.org/en-US/docs/JavaScript/Reference/… Commented Jan 30, 2013 at 22:03
  • 1
    @VadimIvanov That's only if you use Chrome. This will blow up on Firefox (and possibly other browsers). The constructor calls the parse method, which has implementation-dependent behavior. The best bet is to manually parse the date and feed the constructor integer arguments. Or alternatively feed it a universally recognized ISO 8601 date. Commented Jan 30, 2013 at 22:07
  • 1
    You have a datepicker script that does not return a Date object? Or at least has an output option to return a reusable format? Commented Jan 30, 2013 at 22:14
  • 1
    @JoyAcharya Manually parse the output, or use a different date picker library. Commented Nov 15, 2016 at 13:14

3 Answers 3

1

Looks like Date.parse is not going to work because the format the datepicker is returning.

You might want to look at the following thread for parsing solutions or change your format that the datepicker outputs to one of the supported formats.

How can I convert string to datetime with format specification in JavaScript?

http://blog.dygraphs.com/2012/03/javascript-and-dates-what-mess.html

Useful parse info:

http://msdn.microsoft.com/en-us/library/ie/ff743760(v=vs.94).aspx

Sign up to request clarification or add additional context in comments.

4 Comments

The only useful information on that MSDN page is "… is not supported in Internet Explorer 8 standards mode and Quirks mode"
Thanks Jeff, your link has given me the answer! This works using parseDate(date1)
var date1 = new Date(document.getElementById('textbox').value)
Sorry my mistake it was the link provided by kelsey to a blog from Rafael Mueller that gave me the answer! How can I convert string to datetime with format specification in JavaScript?
0

I'd like to recommend you a simple, easy to use and fast library [moment.js][1]

moment("30-Jan-2013").format('MMMM Do YYYY, h:mm:ss a');

will return "January 30th 2013, 12:00:00 am"

Comments

0

Hope this will work for your problem

  var date= document.getElementById('textbox').value.split("-");;
  var date1 = new Date(date[2] + " " + date[1]  + " " + date[0]);
  date1 .toLocaleDateString("en-GB");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.