【问题标题】:need help displaying tomorrows date correctly, javascript需要帮助正确显示明天的日期,javascript
【发布时间】:2020-07-24 06:01:14
【问题描述】:

对于我正在进行的项目,我需要五天的预测。我从明天开始(应该是用户打开应用程序的明天日期)并且日期显示在屏幕上,但它的格式如下(2020 年 7 月 24 日星期五 22:40:10 GMT-0700(太平洋夏令时))。我想要的是这样的格式:Fri Jul 24。

这是我的 javascript:

const tomorrow = new Date().format(ll) 

tomorrow.setDate(new Date().getDate() + 1);

one = document.getElementById('one');
one.innerHTML = tomorrow;

【问题讨论】:

标签: javascript


【解决方案1】:

我只会使用带有正确选项的toLocaleDateString

const formatDate = (d) => {
  const options = { weekday: 'short', month: 'short', day: 'numeric' };
  return d.toLocaleDateString("en-US", options).replace(',','');
}
const tomorrow = new Date()
tomorrow.setDate(new Date().getDate() + 1);
console.log(formatDate(tomorrow));
tomorrow.setDate(tomorrow.getDate() + 1);
console.log(formatDate(tomorrow));
tomorrow.setDate(tomorrow.getDate() + 1);
console.log(formatDate(tomorrow));

【讨论】:

    【解决方案2】:

    通过使用.toDateString()函数可以得到Fri Jul 24 2020的返回值。

    然后你可以使用substr删除最后4个字符。

    这是一个简单的例子:

    const tomorrow = new Date()
    tomorrow.setDate(new Date().getDate() + 1)
    
    console.log(tomorrow.toDateString())
    // "Sat Jul 25 2020"
    console.log(tomorrow.toDateString().substr(0, 10))
    // "Sat Jul 25"
    

    【讨论】:

      猜你喜欢
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      相关资源
      最近更新 更多