简体   繁体   English

moment.js日期将添加,但时间不添加

[英]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. 添加1天,1个月和1年的功能以及清除(myFunction3)均起作用。 The time functions do not. 时间功能没有。 I am starting from 11:59 PM and want to increase hours and minutes separately. 我从晚上11:59开始,希望分别增加小时和分钟。 I know my code is rough, I'm still learning. 我知道我的代码很粗糙,我还在学习。 Thanks 谢谢

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

Your hours addition function should be working, but 'mins' should be spelled as 'minutes' or using shorthand 'm'. 您的小时数附加功能应该可以使用,但“分钟”应拼写为“分钟”或使用速记“ 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. a是一个字符串,因此当您将其传递到moment(a) ,您正在解析该字符串-这是不必要的。 It's also error-prone, since you aren't passing any particular input format as a second parameter to the moment function. 这也容易出错,因为您没有将任何特定的输入格式作为第二参数传递给moment函数。 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. 换句话说-不要混淆stringmoment实例的使用,也不必在不需要时解析字符串。

You make the same mistake in all of your functions. 您在所有功能中都犯了相同的错误。 Additionally, as others pointed out, use either 'minutes' or 'm' , but not 'min' . 另外,正如其他人指出的那样,请使用'minutes''m' ,而不要使用'min' See the documentation for more details. 有关更多详细信息,请参见文档

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

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