Bootstrap Datepicker-如何以正确的格式将日期作为字符串获取? [英] Bootstrap datepicker -- how to get date as a string in the right format?

查看:354
本文介绍了Bootstrap Datepicker-如何以正确的格式将日期作为字符串获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用引导日期选择器.答案可能很明显,但是我无法弄清楚如何以指定的格式将所选日期作为字符串获取.我用过:

I'm using Bootstrap datepicker. The answer is probably obvious, but I can't figure out how to get the selected date as a string in the specified format. I used:

<div id="myDate" data-date-format="yyyy-mm-dd">
  <input type="text">
</div>

在我的javascript中:

And in my javascript:

var value = $("#myDate").datepicker("getDate");

但是它返回日期,而不是yyyy-mm-dd格式的字符串.

But it returns a date, not a string in yyyy-mm-dd format.

推荐答案

需要在输入中设置data-date-format="mm/dd/yyyy":

<input type="text" data-date-format="MM/DD/YYYY">

您的jQuery也应更改为:

Your jQuery should also be changed to:

var value = $("#myDate input").datepicker("getDate");

或者您可以简单地进行简单操作,而无需包装div,并且使用此功能,您原来的jQuery可以正常工作,而您不必指定input:

Or you can just keep things simple with no wrapping div and with this your original jQuery would work fine and you wouldn't have to specify input:

<input type="text" id="myDate" data-date-format="YYYY-MM-DD">

建议
我建议创建一个类来格式化日期选择器.这样您就可以将.datepicker类添加到任何输入字段中,并且无需指定其他变量即可使用它.

Suggestion
I would recommend creating a class to format the datepicker. So that you can then add the .datepicker class to any input field and it would work without having to specify another variable.

小提琴
这是带有日期警报的工作代码 http://jsfiddle.net/doitlikejustin/HFuDg/1/

Fiddle
Here is working code with alert of date http://jsfiddle.net/doitlikejustin/HFuDg/1/

这篇关于Bootstrap Datepicker-如何以正确的格式将日期作为字符串获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆