简体   繁体   中英

moment.js date will add but time does not

I am creating functions to add increase date and time. These are the variables:

    var a = moment().format("MM/DD/YY");
    var j = moment().format("hh:mm A");
    var b = moment(a).format("MM/DD/YY");
    var s = moment(j).format("hh:mm:ss");
    var y = moment({ h: 23, m: 59}).format('hh:mm A');

Here are the functions:

function myFunction() {
    var c = moment(b).add(1, 'day').format("MM/DD/YY");
    document.getElementById("demo2").innerHTML = "<p></p>"+ "X: " +c + "<br>" + y;
     b = c;
}
function myFunction1() {
    var c = moment(b).add(1, 'month').format("MM/DD/YY");
    document.getElementById("demo2").innerHTML ="<p></p>"+ "X: " +c + "<br>" + y;
     b = c;
}
function myFunction2() {
    var c = moment(b).add(1, 'year').format("MM/DD/YY");
    document.getElementById("demo2").innerHTML  ="<p></p>"+ "X: " +c + "<br>" + y;
     b = c;
}
function myFunction3() {
    var c = moment(a).format("MM/DD/YY")
    document.getElementById("demo2").innerHTML  = "<p></p>"+ "X: 00/00/00" + "<br>" + y;
     b = c;
}
function myFunction5() {

    var d = moment(y).add(1, 'hours').format("hh:mm A");
    document.getElementById("demo2").innerHTML  = "<p></p>"+ "X: "+ c + "<br>" + d;
    y = d;
}
function myFunction6() {
    var e = moment(y).add(1, 'min').format('hh:mm A');
    document.getElementById("demo2").innerHTML  = "<p></p>"+ "X: "+ c + "<br>" + e;
    y = e;
}

The functions to add 1 day, 1 month, and 1 year, and the clear (myFunction3) all work. The time functions do not. I am starting from 11:59 PM and want to increase hours and minutes separately. I know my code is rough, I'm still learning. Thanks

Check http://momentjs.com/docs/#/manipulating/add/

Your hours addition function should be working, but 'mins' should be spelled as 'minutes' or using shorthand 'm'.

Thanks for the suggestions. I fixed the minutes in the second function and here is how I got it working. It's something to do with the time formatting.

var y1 = moment({ h: 23, m: 59});
var y = y1.format('hh:mm A');

and the functions then like this:

function myFunction5() {
    var d1 = moment(y1).add(1, 'hours');
    var d = d1.format('hh:mm A');
    document.getElementById("demo2").innerHTML  = "<p></p>"+ "X: "+ b + "<br>" + d;
    y1 = d1;
}
function myFunction6() {
    var e1 = moment(y1).add(1, 'minutes');
    var e = e1.format('hh:mm A');
    document.getElementById("demo2").innerHTML  = "<p></p>"+ "X: "+ b + "<br>" + e;
    y1 = e1;
}

I'm sure there's a better way but it works like this.

You're making the same mistake several times in your code:

var a = moment().format("MM/DD/YY");
var b = moment(a).format("MM/DD/YY");

a is a string, so when you pass it into moment(a) , you're parsing the string - which is unnecessary. It's also error-prone, since you aren't passing any particular input format as a second parameter to the moment function. If you check your log, you'll probably see a bunch of deprecation warnings.

It's also not clear what you're trying to achieve with that, since both strings were in the same output format anyway. If you wanted to get the start of the day, then it would be something like this:

var now = moment();
var a = now.format("...");
var b = now.clone().startOf('day').format("...");

(replacing ... with the format of your choosing)

In other words - don't mix up the use of a string and a moment instance, and don't parse strings when you don't have to.

You make the same mistake in all of your functions. Additionally, as others pointed out, use either 'minutes' or 'm' , but not 'min' . See the documentation for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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