1

I have a date of format YYYY-MM-DD, and I want to display the 28th day from the given day. I tried some ways but couldn't fix it. If possible, I'd like a solution without using Moment.js

Jsfiddle

If there is no other solution, can someone help me use Moment.js?

TrojanByAccident
  • 227
  • 1
  • 6
  • 20
H Varma
  • 570
  • 4
  • 12
  • 29

6 Answers6

6

Here's a function that I turned into a Date prototype that handles date offsets:

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


d = new Date
d.addDays(28)
Oliver
  • 198
  • 4
  • 11
  • Does addDays() exists on the date object ?https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date – rckrd Dec 21 '16 at 18:16
  • `addDays` is not a standard method on the date object in any javascript implementation. So no one will have this method. – rckrd Dec 21 '16 at 18:23
  • 1
    @rckrd I have edited my post. I must have had that function saved into my Date prototype from earlier. – Oliver Dec 21 '16 at 18:26
6
    var date =new Date("2016-08-30");    
    var newDate = new Date(date.getFullYear(),date.getMonth(),date.getDate()+28)
Amaldev ps
  • 257
  • 2
  • 12
2

So you basically want to add 28 days to the given date? Then this is for you.

var d = new Date(2016, 7, 30)  // parsing dates is a different task
// d equals Tue Aug 30 2016 00:00:00 GMT+0200 (CEST)
var offset = 28 * 24 * 60 * 60 * 1000;
var newDate = new Date(d.getTime() + offset)
// newDate equals Tue Sep 27 2016 00:00:00 GMT+0200 (CEST)

In case you also want to parse / stringify the date you could use:

// parse date
var dateStr = '2016-08-30';
var date = new Date(dateStr);

// stringify date
date.toISOString().split('T')[0]
dotcs
  • 2,286
  • 1
  • 18
  • 26
1

java script :

var today = new Date();
var noDaysToAdd = 28;
someDate.setDate(today() + noDaysToAdd); 
Formatting to YYYY-MM-DD :
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();

var someFormattedDate = y + '-' + mm + '-' + dd ;
jAvA
  • 557
  • 1
  • 8
  • 22
1

You can do something like this

 var d=new Date("2016-08-30");
 var n=28; //number of days to add. 
 $scope.renewaldate=new Date(d.getFullYear(),d.getMonth(),d.getDate()+n);

JSFIDDLE

Initializing the date variable

For UTC time

Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time.

Non-UTC

Get timezone using getTimezoneOffset() and then set the time

var d = new Date(xiYear, xiMonth, xiDate);
d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );
Naghaveer R
  • 2,890
  • 4
  • 30
  • 52
  • Using the Date constructor (or Date.parse) to parse an ISO 8601 format date may result in a date that appears to be the previous day for users in a timezone west of Greenwich. – RobG Dec 21 '16 at 22:25
  • @RobG Yes, I agree. I have updated my answer. – Naghaveer R Dec 22 '16 at 05:58
0

I'd suggest converting your date to a timestamp, adding 100 * 60 * 60 * 24 * 28 (the number of milliseconds in 28 days) to it, and converting it back to a human-readable date.

var now = new Date(
  (new Date()).valueOf()
  + 1000 * 60 * 60 * 24 * 28
)

and then you will need to format it as

console.log(now.getFullYear()
            + "-"
            + (now.getMonth() + 1) 
            + "-"
            + (now.getDays() + 1)
)

The + 1 is because JavaScript gives days and months as between 0 to 11 rather than 1 to 12.

There is still the problem that you are getting 5 instead of 05 so you might need to write yourself a function like

function forceTwoDigits(val){
    val = String(val) // Force value to be a string.
    if(val.length == 1) return "0" + val;
    return val; // If it hasn't returned yet
}
Max
  • 1,325
  • 9
  • 20