jQuery 日期选择器显示格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8672382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:52:39  来源:igfitidea点击:

jQuery datepicker display format

jquerydatepickerformat

提问by Dmitry

I have MySQL table with date field (yy-mm-dd).

我有带有日期字段 (yy-mm-dd) 的 MySQL 表。

And I have form with datepicker that edits it and it works fine.

我有一个带有日期选择器的表单,可以编辑它并且它工作正常。

BUT at the frontend date is displayed as mm-dd-yy and that can be confusing.

但是在前端日期显示为 mm-dd-yy,这可能会令人困惑。

I'd like field with datepicker to display value as mm-dd-yy, but the actual value should be of the old format (yy-mm-dd).

我希望带有日期选择器的字段将值显示为 mm-dd-yy,但实际值应该是旧格式 (yy-mm-dd)。

How to reach this?

如何达到这一点?

Added: I know how to change format of date. I want separatedisplayed value and actual value. That's what it is all about.

补充:我知道如何更改日期格式。我想要单独的显示值和实际值。这就是它的全部意义。

回答by Jasper

The jQuery UI Datepicker widget comes with altFieldand altFormatoptions that allow you to set a secondary (hidden if you want) form input to store the date in whatever format you want:

在jQuery UI的日期选择器插件自带altFieldaltFormat选项,允许你设置一个二级(隐藏的,如果你想)形式输入到存储在你想要的任何格式的日期:

$( ".selector" ).datepicker({
    altField  : '#actualDate',
    altFormat : 'yy-mm-dd'
});

Source: http://jqueryui.com/demos/datepicker/#option-altFormat

来源:http: //jqueryui.com/demos/datepicker/#option-altFormat

Here is a demo: http://jsfiddle.net/MmKmq/

这是一个演示:http: //jsfiddle.net/MmKmq/

回答by Ric

Have a look at this jquery page, it will direct you to what you want:

看看这个 jquery 页面,它会引导你到你想要的:

http://docs.jquery.com/UI/Datepicker#option-dateFormat

http://docs.jquery.com/UI/Datepicker#option-dateFormat

as for processing it as yy-mm-dd you may have to do this through a method on your web page to rearrange the formatting.

至于将其处理为 yy-mm-dd,您可能必须通过网页上的方法来重新排列格式。

回答by Mr.J4mes

You can try this:

你可以试试这个:

<input type="text"   id="displayDate" />
<input type="hidden" id="submittedDate" />

<script type="text/javascript">

   $(document).ready(function() {

      $("#displayDate").datepicker({
         dateFormat: 'MM dd, yy', // this is the format of the date that will be shown
         onSelect: function(dateText, inst) {
            var date  = new Date(dateText);
            $("#submittedDate").val(date.getTime()); // transform into what you want to submit here
         }
      });

   });

</script>