用于格式化最后一天的JavaScript

时间:2017-02-26 05:50:54

标签: javascript date

我可以显示当前月份的最后一天,或者我可以按照我想要的方式显示当前日期,但不能同时显示两者。例如,在二月我希望脚本生成2/28/2017;三月它会产生3/31/2017; 2017年4月4日至30日;等



endpoints:
  enabled: false # Default enabled/disabled on endpoints on Spring Boot Actuator
  health: # Health check (already prepared in Spring Boot Actuator)
    enabled: true
  httpcheck: # Simple HTTP connection check (newly created by myself)
    enabled: true

function ShowDate0() {
  var date = new Date(),
    y = date.getFullYear(),
    m = date.getMonth();
  var lastDay = new Date(y, m + 1, 0);
  document.getElementById("showdate0").innerHTML = lastDay;
}

function ShowDate1() {
  var date = new Date(),
         y = date.getFullYear(),
         m = date.getMonth();
  var lastDay = date.getMonth() + "/" + date.getDate() + "/" + date.getFullYear();
  document.getElementById("showdate1").innerHTML = lastDay;
}
window.onload = function(){
  ShowDate0();
  ShowDate1();
}




因此我认为这可以用m / d / yyyy格式显示月份的最后一天,但月份落后(显示2017年1月28日时应该是2/28/2017):



<b>Last Day with verbose formatting (Day-of-week Month Day Year Time GMT-Time Time Zone):</b>
<p id="showdate0"></p>

<b> Current date with desired formatting(m / d / yyyy): </b>
<p id="showdate1"></p>
&#13;
function ShowDate() {
  var date = new Date(),
    y = date.getFullYear(),
    m = date.getMonth();
  var lastDay = new Date(y, m + 1, 0);
  var formatted = lastDay.getMonth() + "/" + lastDay.getDate() + "/" + lastDay.getFullYear();
  document.getElementById("showdate").innerHTML = formatted;
}
window.onload = ShowDate;
&#13;
&#13;
&#13;

我该如何解决这个问题?

0 个答案:

没有答案