[英]need help displaying tomorrows date correctly, javascript
對於我正在進行的一個項目,我需要一個五天的預測。 我從明天開始(應該是用戶打開應用程序的明天日期)並且日期顯示在屏幕上,但它的格式如下(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;
我只會使用帶有正確選項的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));
通過使用.toDateString()
function 您可以獲得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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.