Skip to content Skip to sidebar Skip to footer

Manipulating Date/time With Js And Moment

I have 2 dates and I need to create a 3rd date using those 2 dates. if (document.getElementById('endDate')) endDate = document.getElementById('endDate').value; if (document.getEle

Solution 1:

1: add/subtract days: new Date(new Date().setDate(new Date().getDay() + 3))

2: add/subtract months: new Date(new Date().setMonth(new Date().getMonth() + 3))

//your codevar presentDt = newDate(presentDate);
var endDt = newDate(endDate);

var newDay = newDate(newDate().setDate(endDt.getDay() + 2)); 
var presentMonth = presentDt.getUTCMonth();
var presentYear = presentDt.getUTCFullYear();

Solution 2:

Since your input string is not in a format recognized by new Date() across environments, I suggest to use moment(String, String) to parse it.

Then you can use moment(Object), moment's setters (like year(), month() and date()) and format() to get the desired output.

Here a live sample:

var presentDt = "12/5/2018";
var endDt = "12/25/2018";

// Parse your input with momentjsvar mPresent = moment(presentDt, "MM/DD/YYYY");
var mEnd = moment(endDt, "MM/DD/YYYY");

// Create a new momnt object compliant with your needsvar nextDate = moment({
  year: mPresent.year(), // get presentDt's yearmonth: mPresent.month(), // get presentDt's monthdate: mEnd.add(1, 'day').date() // get endDt day of the month and add 1 day to it
});

// Display the result in the format you needconsole.log(nextDate.format("MM/DD/YYYY"));
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Note that you will get 01 as day result when endDt represents the last day of the month.

Solution 3:

This is what i did:

First i created the following functions to look at the number of days and months.

functionformattedDate(date) {
        var day = date.getDate();
        var monthIndex = date.getMonth();
        var year = date.getFullYear();

        return (monthIndex + 1) + '/' + day + '/' + year;
    }
    functionmyNewDate(datestring) {
        var dates = datestring.split("/");
        var year, month, day;
        if (dates.length < 3) {
            dates = datestring.split("-");
            year = dates[0];
            month = dates[1] - 1;
            day = dates[2];
        } else  {
            year = dates[2];
            month = dates[0] - 1;
            day = dates[1];
        }


        var days = [31, 28, 31, 30, 31, 30 ,31, 31, 30, 31, 30, 31];
        // Overflow dayif (day > days[month])
            day = days[month];

        returnnewDate(year, month, day);
    }
    functionnewDate(date1, date2) {
        var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        var year = date1.getFullYear();
        var month = date1.getMonth() + 1;
        var day = date2.getDate() + 1;


        // Check leap yearif ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
            days[1] = 29;

        // Overflow dayif (day > days[month])
            day = days[month];

        var newdate = newDate(year, month, day);

        return newdate;
    }

Here, i used the above function to create my new date. You can also add/subtract days/months as you want by updating the above.

var mpresent = myNewDate(presentDate);
        var mstart = myNewDate(startDate);
        var myDate =  (formattedDate(newDate(mpresent, mstart)));

Post a Comment for "Manipulating Date/time With Js And Moment"