在我约会的日期前面添加今天和明天的日期

时间:2017-08-04 17:24:11

标签: javascript html

小提琴:https://jsfiddle.net/kxqndpk1/2/

    var months = ['January','February','March','April','May','June','July',
    'August','September','October','November','December'];       
    var today = new Date();
    today.setTime(today.getTime());   
    document.getElementById("spanToday").innerHTML = months[today.getMonth()] + " " + today.getDate()+ ", " + today.getFullYear();
    
    
    var months = ['January','February','March','April','May','June','July',
    'August','September','October','November','December'];       
    var tomorrow = new Date();
    tomorrow.setTime(tomorrow.getTime()  + (1000*3600*24));   
    document.getElementById("spanTomorrow").innerHTML = months[tomorrow.getMonth()] + " " + tomorrow.getDate()+ ", " + tomorrow.getFullYear();
    <span id="spanToday"></span>, at 10:00 AM
    <br>
    <span id="spanTomorrow"></span>, at 10:00 AM
    <br><br>
    Eastern Time (EST)

目标是在2个日期前添加适当的日期。

4 个答案:

答案 0 :(得分:1)

使用getDay()

var months = ['January','February','March','April','May','June','July',
    'August','September','October','November','December'];   
    var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    var today = new Date();
    today.setTime(today.getTime());   
    document.getElementById("spanToday").innerHTML = days[today.getDay()] + "  " + months[today.getMonth()] + " " + today.getDate()+ ", " + today.getFullYear();
    
     
    var tomorrow = new Date();
    tomorrow.setTime(tomorrow.getTime()  + (1000*3600*24));   
    document.getElementById("spanTomorrow").innerHTML =days[tomorrow.getDay()] + "  " + months[tomorrow.getMonth()] + " " + tomorrow.getDate()+ ", " + tomorrow.getFullYear();
<span id="spanToday"></span>, at 10:00 AM
    <br>
    <span id="spanTomorrow"></span>, at 10:00 AM
    <br><br>
    Eastern Time (EST)

答案 1 :(得分:0)

您可以按照上述类似的步骤进行操作:

例如:

 var d = new Date();
    var weekday = new Array(7);
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";

    var n = weekday[d.getDay()];

现在n包含日期信息

答案 2 :(得分:0)

var weekday = new Array(7);
weekday[0] =  "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";

document.getElementById("spanToday").innerHTML = weekday[today.getDay()] + " " + months[today.getMonth()] + " " + today.getDate()+ ", " + today.getFullYear();

答案 3 :(得分:0)

如果您使用的是现代环境,则可以使用The ECMAScript Internationalization API,特别是DateTimeFormat

&#13;
&#13;
const now = new Date();
const tomorrow = new Date(now.getTime() + 86400000);
const options = {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric'
};
const dateFormater = new Intl.DateTimeFormat('en-US', options);
const spanToday = document.getElementById('spanToday');
spanToday.textContent = dateFormater.format(now);
const spanTomorrow = document.getElementById('spanTomorrow');
spanTomorrow.textContent = dateFormater.format(tomorrow);
&#13;
<span id="spanToday"></span>, at 10:00 AM
<br>
<span id="spanTomorrow"></span>, at 10:00 AM
<br>
<br> Eastern Time (EST)
&#13;
&#13;
&#13;

There is a polyfill available for older environments.