Converting Javascript String to Datetime - Stack Overflow

I have a text box that is populated from a datepicker in Javascript.The date is stored as 30-Jan-2013

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?

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?

Share Improve this question edited Jan 30, 2013 at 22:11 Jeff 12.8k5 gold badges35 silver badges62 bronze badges asked Jan 30, 2013 at 22:00 APWAPW 591 gold badge4 silver badges11 bronze badges 12
  • 1 Are you trying to parse the date using JavaScript or ASP.NET? – NullUserException 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/en-US/docs/JavaScript/Reference/… – broofa 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. – NullUserException 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? – Bergi Commented Jan 30, 2013 at 22:14
  • 1 @JoyAcharya Manually parse the output, or use a different date picker library. – Bergi Commented Nov 15, 2016 at 13:14
 |  Show 7 more ments

3 Answers 3

Reset to default 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./2012/03/javascript-and-dates-what-mess.html

Useful parse info:

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

I'd like to remend 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"

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");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745266032a4619461.html

相关推荐

  • Converting Javascript String to Datetime - Stack Overflow

    I have a text box that is populated from a datepicker in Javascript.The date is stored as 30-Jan-2013

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信