如何让Javascript键入一个月的名字?

时间:2016-12-16 13:23:28

标签: javascript datepicker



$(document).ready(function () {
            $('#txtDate').datepicker();
        });

        function getdate() {
            var tt = document.getElementById('txtDate').value;

            var date = new Date(tt);
            var newdate = new Date(date);
            //Second Date
            newdate.setDate(newdate.getDate() + 1);

            var dd = newdate.getDate();
            var mm = newdate.getMonth() + 1;
            var y = newdate.getFullYear();

            var someFormattedDate = dd + '/' + mm + '/' + y;
            document.getElementById('second_date').value = someFormattedDate;

            //Third Date
            newdate.setDate(newdate.getDate() + 1);

            var dd = newdate.getDate();
            var mm = newdate.getMonth() + 1;
            var y = newdate.getFullYear();

            var someFormattedDate = dd + '/' + mm + '/' + y;
            document.getElementById('third_date').value = someFormattedDate;
          }

.ui-datepicker {
            background: #fff;
            width: auto;
            height: auto;
            margin: 5px auto 0;
            font: 10pt Verdana, Arial, sans-serif;
            border: 1px solid #b4b3b3;
        }

 <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

Date:
    <input id="txtDate" type="text" />


    <input type="button" onclick="getdate()" value="Fill Follow Date" />
    <br />
    <br />
    Follow Date:
    <p>
        2nd day:
    <input id="second_date" type="text" />
    </p>
    <p>
        3rd day:
    <input id="third_date" type="text" />
    </p>
&#13;
&#13;
&#13;

我有上面的代码,我想知道你是否有任何线索如何让它键入月份的名称,而不仅仅是数字?

P.S。我是一个全新的人,我知道你们都是先进的,但是我试图解决这个问题,但我无法做到。

编辑:在这里看到问题:Get month name from Date但是在这种情况下无法弄清楚如何添加它...

3 个答案:

答案 0 :(得分:2)

您是否考虑过创建一个(本地化)名称数组:

var months = ['Jan', 'Feb', ... etc];

然后只需将其编入索引:

var currentMonth = months[new Date().getMonth()];

否则你可能会看到像MomentJS

这样的库

答案 1 :(得分:0)

使用此

var months = [ "January", "February", "March", "April", "May", "June", 
               "July", "August", "September", "October", "November", "December" ];

var selectedMonthName = months[$("#datepicker").datepicker('getDate').getMonth()];

答案 2 :(得分:0)

你可以这样试试,

$(document).ready(function () {
            $('#txtDate').datepicker({
            dateFormat: 'dd/M/yy'});
        });

        function getdate() {
            var tt = document.getElementById('txtDate').value;

            var date = new Date(tt);
            var newdate = new Date(date);
            //Second Date
            newdate.setDate(newdate.getDate() + 1);

           
           document.getElementById('second_date').value = newdate.toLocaleDateString('en-GB', {  
    day : 'numeric',
    month : 'short',
    year : 'numeric'
}).split(' ').join('/');

            //Third Date
            newdate.setDate(date.getDate() + 2);


            document.getElementById('third_date').value = newdate.toLocaleDateString('en-GB', {  
    day : 'numeric',
    month : 'short',
    year : 'numeric'
}).split(' ').join('/');
          }
.ui-datepicker {
            background: #fff;
            width: auto;
            height: auto;
            margin: 5px auto 0;
            font: 10pt Verdana, Arial, sans-serif;
            border: 1px solid #b4b3b3;
        }
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

Date:
    <input id="txtDate" type="text" />


    <input type="button" onclick="getdate()" value="Fill Follow Date" />
    <br />
    <br />
    Follow Date:
    <p>
        2nd day:
    <input id="second_date" type="text" />
    </p>
    <p>
        3rd day:
    <input id="third_date" type="text" />
    </p>