简体   繁体   English

需要帮助正确显示明天的日期,javascript

[英]need help displaying tomorrows date correctly, javascript

For a project I'm working on I need a five day forecast.对于我正在进行的一个项目,我需要一个五天的预测。 I'm starting out with tomorrow (which should be tomorrows date of whenever the user opens the app) and the date is appearing on the screen but it is formatted like this (Fri Jul 24 2020 22:40:10 GMT-0700 (Pacific Daylight Time)).我从明天开始(应该是用户打开应用程序的明天日期)并且日期显示在屏幕上,但它的格式如下(2020 年 7 月 24 日星期五 22:40:10 GMT-0700(太平洋夏令时))。 What I would like is a format like this: Fri Jul 24.我想要的是这样的格式:Fri Jul 24。

Here is my javascript:这是我的 javascript:

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

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

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

I would just use toLocaleDateString with the right options:我只会使用带有正确选项的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));

By using .toDateString() function you can get return of Fri Jul 24 2020 .通过使用.toDateString() function 您可以获得Fri Jul 24 2020的回报。

Then you can delete the last 4 characters using substr .然后您可以使用substr删除最后 4 个字符。

Here is a simple example:这是一个简单的例子:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM