gpt4 book ai didi

javascript - 日期选择器 - 仅将 YY 和 MM 分开

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:29 25 4
gpt4 key购买 nike

我正在尝试为 YY 和 MM 设置两个单独的字段我现在拥有的或多或少是我想要的,只是它只是一个领域。这是它现在的样子:how it's looking right now

 <input id="residenceyears" name="residenceyears" type="text"    placeholder="YY/MM" class="input yyMM numeric-only">

这是 JS 函数:

<script type="text/javascript">
$('.yyMM').datepicker({
changeMonth: true,
changeYear: true,
monthNames: ["1","2","3","4","5","6","7","8","9","10","11","12"],
yearRange: "-100:+0",
dateFormat: 'yy/mm',

onClose: function() {
var iMonth = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var iYear = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(iYear, iMonth, 1));
},

beforeShow: function() {
if ((selDate = $(this).val()).length > 0)
{
iYear = selDate.substring(selDate.length - 4, selDate.length);
iMonth = jQuery.inArray(selDate.substring(0, selDate.length - 5), $(this).datepicker('option', 'monthNames'));
$(this).datepicker('option', 'defaultDate', new Date(iYear, iMonth, 1));
$(this).datepicker('setDate', new Date(iYear, iMonth, 1));
}
}

});

我想要实现的是:it should look like this

最佳答案

看看这个 jsFiddle -> https://jsfiddle.net/mLcpwdra/

代码:

html

<div id="datepicker"></div>
<hr />
<div id="month"><p>Month:<span></span></p></div>
<div id="year"><p>Year:<span></span></p></div>
<div id="day"><p>Day:<span></span></p></div>

js

var obj = {
init: function()
{
$('#datepicker').datepicker({
onSelect : function(date_string,instance)
{
var date_obj = new Date(date_string);
$('#month span').text(String("00"+date_obj.getMonth()).slice(-2));
$('#year span').text(date_obj.getFullYear());
$('#day span').text(String("00"+date_obj.getDate()).slice(-2));
}
});
}
};

$(document).ready(function(){
obj.init();
});

这是一个起点,但认为适合您的需求。希望这有帮助

更新:以 YY“格式”修改年份 -> https://jsfiddle.net/mLcpwdra/1/

关于javascript - 日期选择器 - 仅将 YY 和 MM 分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38497441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com