简体   繁体   English

如何在 JavaScript 中为当前日期和格式添加天数?

[英]How to add days to a current date and format in JavaScript?

I want to make a function that takes today's date and add more days.我想制作一个需要今天日期并添加更多天数的函数。 For example, if todays' date is 10/09/20 and I add 5 days I want to return 15/09/20.例如,如果今天的日期是 10/09/20 并且我添加了 5 天,我想返回 15/09/20。

I want to format the result as such:我想这样格式化结果:

15 Sep 9月15日

I've created the following function:我创建了以下函数:

function calcDate(days){
  var curDate = new Date();
  
  var estDate = curDate.setDate(curDate.getDate() + days);
 
  return estDate.getDate() + ' ' + estDate.getMonth();
}

However, I get the error estDate.getDate() is not a function .但是,我收到错误estDate.getDate() is not a function

If I just return estDate I also get an unformatted number, eg: 1608685587862如果我只返回estDate我也会得到一个未格式化的数字,例如: 1608685587862

I've tried several approaches from Google and Stack Overflow but none work.我尝试了来自 Google 和 Stack Overflow 的几种方法,但都没有奏效。

Would anyone know what I am to do?有谁知道我要做什么?

Date.prototype.setDate returns the milliseconds of the result, which is a number, not Date object. Date.prototype.setDate返回结果的毫秒数,它是一个数字,而不是Date对象。

You can also instead add the equivalent milliseconds of those days to the current time to calculate the desired date:您还可以将这些天的等效毫秒数添加到当前时间以计算所需的日期:

 function calcDate(days){ var curDate = new Date(); var estDate = new Date(curDate.getTime() + days * 24 * 60 * 60 * 1000); return estDate.toLocaleDateString('en-GB', { month: 'short', day: 'numeric' }); } console.log(calcDate(5));

Almost there!差不多好了! You're using the correct date methods: .setDate() .您正在使用正确的日期方法: .setDate() It's just the formatting that's left.这只是剩下的格式。 You can use Moment JS to format the date.您可以使用 Moment JS 来格式化日期。

 function calcDate(days){ var curDate = new Date(); var estDate = curDate.setDate(curDate.getDate() + days); return moment(estDate).format('DD/MM/YY'); } console.log( calcDate( 5 ) ); console.log( calcDate( 10 ) ); console.log( calcDate( 20 ) );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js" integrity="sha512-rmZcZsyhe0/MAjquhTgiUcb4d9knaFc7b5xAfju483gbEXTkeJRUMIPk6s3ySZMYUHEcjKbjLjyddGWMrNEvZg==" crossorigin="anonymous"></script>

setDate() will return date value in milleseconds, you need to parse it again as date. setDate() 将以毫秒为单位返回日期值,您需要再次将其解析为日期。 In your example you no need to use an additional variable "estdate" you can use the " curDate " variable after you set the date.在您的示例中,您无需使用额外的变量“estdate”,您可以在设置日期后使用“ curDate ”变量。

Note: Date.getMonth() will return zerobased month ie for september it will return 8.注意: Date.getMonth() 将返回从零开始的月份,即 9 月它将返回 8。

function calcDate(days){
  var curDate = new Date(); 
 curDate.setDate(curDate.getDate() + days); 
  return curDate.getDate() + ' ' + (curDate.getMonth()+1);
}
console.log(calcDate(1));

Here is the Demo (See JavaScript Tab)这是演示(请参阅 JavaScript 选项卡)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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