Home > Blockchain >  How to add days to today? javascript
How to add days to today? javascript

Time:07-11

How to add days to today in day-month-year format? I tried this code but additionally get the time zone and month in the short word name. I want to receive, for example, August 12, 2023 here is the code:

Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate()   days);
return date;
}

var date = new Date();

console.log(date.addDays(5));

CodePudding user response:

To get the format: Month Day, Year, Simply use ECMAScript Internationalization API:

return date.toLocaleString('en-us',{month:'long', year:'numeric', day:'numeric'})
month:'long' //August
day:'numeric' //12
year:'numeric' //2023

Note: 'long' uses the full name of the month, 'short' for the short name,

CodePudding user response:

Create an array of months

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

In the function Date.prototype.addDays, return the date string as

return months[date.getMonth()]   " "   date.getDate()   ", "   date.getFullYear();

Output:

July 15, 2022

CodePudding user response:

You are adding 2 dates in a correct way, there is only problem in outputting the error. You can format date as follow based on this answer:

Date.prototype.addDays = function(days) {
  var date = new Date(this.valueOf());
  date.setDate(date.getDate()   days);
  
  // return formatted date
  var options = {year: 'numeric', month: 'long', day: 'numeric' };
  return date.toLocaleDateString("en-US", options);
}

var date = new Date();

console.log(date.addDays(5));

CodePudding user response:

Your question is twofold:

  1. how to add days to a date
  2. and how to format a date

1. adding days

the easiest way to do simple calculations with date and time values is to transform all date and time into UNIX timestamps (number of milliseconds since the january 1st 1970 epoch) then adding and substracting days or calculating date differences is simply adding and substracting integer numbers.

For example.

to obtain the unix time of right now just use Date.now() which is 1657452836534
five days in millisecods is 5*24*60*60*1000
thus five days ahead of now is Date.now() 5*24*60*60*1000 which is 1657884836534
to obtain a Date object, you need to create one with the timestamp value new Date(Date.now() 5*24*60*60*1000)

2. format Date

formatting Dates is more complex that it seems, as there are some subtle issues around, depending on users cultural conventions. See How do I format a date in JavaScript? for a detailed answer. But as a show answer if you want your code to work for any user, the easiest way it to use native javascript toLocaleDateString() function. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString .

wrapping up, for your case, try

new Date(Date.now() 5*24*60*60*1000).toLocaleDateString()
  • Related