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?

Jeff
  • 12,555
  • 5
  • 33
  • 60
APW
  • 59
  • 1
  • 4
  • 11
  • 1
    Are you trying to parse the date using JavaScript or ASP.NET? – NullUserException Jan 30 '13 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 https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse – broofa Jan 30 '13 at 22:03
  • something is wrong, because that code not suppose to return Nan, just try var date = new Date("30-Jan-2013") it will return "Wed Jan 30 2013 00:00:00 GMT+0000 (WET)". so, on time when you call this code your textbox doesn't have a value – Vadim Ivanov Jan 30 '13 at 22:04
  • 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.](http://en.wikipedia.org/wiki/ISO_8601) – NullUserException Jan 30 '13 at 22:07
  • Here's a fiddle running through some date formats (strings) and converting them to Date objects. Results vary by browser. Good luck! http://jsfiddle.net/kboucher/MgdQa/ – Kevin Boucher Jan 30 '13 at 22:08
  • Check out [Problem with date formats in JavaScript with different browsers](http://stackoverflow.com/q/3566125/781965) – Jeff Jan 30 '13 at 22:10
  • 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? – Bergi Jan 30 '13 at 22:14
  • Thanks for the quick responses, I am using IE and I want the code in JS. Vadim, maybe I do not unserstand but I do not want to pass a hard coded date in as the date is picked by the user and it is not necessarily going to be todays date? – APW Jan 30 '13 at 22:15
  • I have the same problem. and i have datepicker script that does not return a Date object. So how to fixed it @Bergi – Joy Acharya Nov 15 '16 at 07:29
  • works only for chrome – Joy Acharya Nov 15 '16 at 07:43
  • 1
    @JoyAcharya Manually parse the output, or use a different date picker library. – Bergi Nov 15 '16 at 13:14
  • @Bergi thanks i have solved it already . :) – Joy Acharya Nov 16 '16 at 09:26

3 Answers3

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

Community
  • 1
  • 1
Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • 1
    The only useful information on that MSDN page is "*… is not supported in Internet Explorer 8 standards mode and Quirks mode*" – Bergi Jan 30 '13 at 22:16
  • Thanks Jeff, your link has given me the answer! This works using parseDate(date1) – APW Jan 30 '13 at 22:29
  • var date1 = new Date(document.getElementById('textbox').value) – APW Jan 30 '13 at 22:51
  • 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? – APW Jan 30 '13 at 22:58
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"

Vadim Ivanov
  • 633
  • 1
  • 7
  • 16
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");
Joy Acharya
  • 680
  • 1
  • 10
  • 18