如何从下拉列表中格式化日期?

时间:2016-10-21 02:51:52

标签: javascript date-format

我想问一下如何将selectmonth的月份格式更改为"九月"而不是09

function myFunction() {
var d = new Date();
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var x = month[d.document.getElementById("selectmonth").value];
var y = document.getElementById("selectyear").value;
document.getElementById("demo").innerHTML = "Quick summary for " + x + y ;
}

4 个答案:

答案 0 :(得分:1)

我认为你应该改变价值 他自己的html标签selectmonth。

window.onload = function(){
    document.getElementById("select").onchange = function () {
        var get={};
        get.x=function () {
            return document.getElementById("selectmonth").value;
        }
        get.y=function () {
            return document.getElementById("selectyear").value;
        }
        document.getElementById("demo").innerHTML =
                "Quick summary for " + get.x() + get.y();
    }
}
<div id="select">
    <select id="selectmonth">
        <option name="month" value="">Select...</option>
        <option name="month" value="January">January</option>
        <option name="month" value="February">February</option>
        <option name="month" value="March">March</option>
        <option name="month" value="April">April</option>
        <option name="month" value="May">May</option>
        <option name="month" value="June">June</option>
        <option name="month" value="July">July</option>
        <option name="month" value="August">August</option>
        <option name="month" value="September">September</option>
        <option name="month" value="October">October</option>
        <option name="month" value="November">November</option>
        <option name="month" value="December">December</option>
    </select>
    <select id="selectyear">
        <option name="year" value="2016" selected>2016</option>
        <option name="year" value="2017">2017</option>
        <option name="year" value="2018">2018</option>
    </select>
</div>
<div id="demo"></div>

想要帮助你。

答案 1 :(得分:1)

您在评论中说您的选择选项会显示月份名称,因此可能意味着您的选项如下:

<option value="09">September</option>

如果是这样,那么您可以获得所选项目的值并显示如下名称:

var monthDropdown = document.getElementById("selectmonth");
var x = monthDropdown.value
var monthName = monthDropdown.options[monthDropdown.selectedIndex].textContent;

在上下文中:

function myFunction() {
  var monthDropdown = document.getElementById("selectmonth");
  var x = monthDropdown.value
  var monthName = monthDropdown.options[monthDropdown.selectedIndex].textContent;
  var y = document.getElementById("selectyear").value;
  document.getElementById("demo").innerHTML = "Quick summary for " + monthName + " " + y + " (the month number was " + x + ")";
}
document.querySelector("button").addEventListener("click", myFunction);
<select id="selectmonth">
  <option value="01">January</option>
  <option value="02">February</option>
  <option value="03">March</option>
  <option value="04">April</option>
  <option value="05">May</option>
  <option value="06">June</option>
  <option value="07">July</option>
  <option value="08">August</option>
  <option value="09">September</option>
  <option value="10">October</option>
  <option value="11">November</option>
  <option value="12">December</option>
</select>
<input id="selectyear" value="2016">
<button>Test</button>
<div id="demo"></div>

答案 2 :(得分:0)

function myFunction() {
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var x = month[parseInt(document.getElementById("selectmonth").value,10)];
var y = document.getElementById("selectyear").value;
document.getElementById("demo").innerHTML = "Quick summary for " + x + y ;
}

答案 3 :(得分:0)

只需创建一个包含所有月份的数组,然后按索引引用数组。

function myFunction() {

  var month = ["January",
               "February",
               "March",
               "April",
               "May",
               "June",
               "July",
               "August",
               "September",
               "October",
               "November",
               "December"]
  
  var x = document.getElementById("selectmonth").value;
  var y = document.getElementById("selectyear").value;
 
  document.getElementById("demo").innerHTML = "Quick summary for " + month[x - 1] + y ;
}
<input type="text" id="selectmonth" value="9"/>
<input type="text" id="selectyear" value="2016"/>
<div id="demo"></div>
<input type="button" onclick="myFunction()" value="TestMe" />