Javascript 使用 Moment.js 格式化日期和减去天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29651815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:43:48  来源:igfitidea点击:

Format date and Subtract days using Moment.js

javascriptdatemomentjs

提问by beaumondo

I would like a variable to hold yesterday's date in the format DD-MM-YYYYusing Moment.js. So if today is 15-04-2015, I would like to subtract a day and have 14-4-2015.

我想要一个变量来DD-MM-YYYY使用 Moment.js以格式保存昨天的日期。所以如果今天是 15-04-2015,我想减去一天得到 14-4-2015。

I've tried a few combinations like this:

我尝试了一些这样的组合:

startdate = moment().format('DD-MM-YYYY');
startdate.subtract(1, 'd');

and this:

和这个:

startdate = moment().format('DD-MM-YYYY').subtract(1, 'd');

and also this:

还有这个:

startdate = moment();
startdate.subtract(1, 'd');
startdate.format('DD-MM-YYYY')

But I'm not getting it...

但我不明白...

回答by ndugger

You have multiple oddities happening. The first has been edited in your post, but it had to do with the order that the methods were being called.

你有很多奇怪的事情发生。第一个已在您的帖子中进行了编辑,但它与调用方法的顺序有关。

.formatreturns a string. String does not have a subtractmethod.

.format返回一个字符串。字符串没有subtract方法。

The second issue is that you are subtracting the day, but not actually saving that as a variable.

第二个问题是您正在减去一天,但实际上并未将其保存为变量。

Your code, then, should look like:

那么,您的代码应如下所示:

var startdate = moment();
startdate = startdate.subtract(1, "days");
startdate = startdate.format("DD-MM-YYYY");

However, you can chain this together; this would look like:

但是,您可以将它们链接在一起;这看起来像:

var startdate = moment().subtract(1, "days").format("DD-MM-YYYY");

The difference is that we're setting startdate to the changes that you're doing on startdate, because moment is destructive.

不同之处在于我们将 startdate 设置为您在 startdate 上所做的更改,因为 moment 是破坏性的。

回答by M. Adam Kendall

startdate = moment().subtract(1, 'days').format('DD-MM-YYYY');

回答by M. Adam Kendall

Try this:

尝试这个:

var duration = moment.duration({'days' : 1});
moment().subtract(duration).format('DD-MM-YYYY');

This will give you 14-04-2015- today is 15-04-2015

这会给你14-04-2015-今天是 15-04-2015

Alternatively if your momentjs version is less than 2.8.0, you can use:

或者,如果您的 momentjs 版本低于 2.8.0,您可以使用:

startdate = moment().subtract('days', 1).format('DD-MM-YYYY');

Instead of this:

取而代之的是:

startdate = moment().subtract(1, 'days').format('DD-MM-YYYY');

回答by Jethik

startdate = moment().subtract(1, 'days').startOf('day')

startdate = moment().subtract(1, 'days').startOf('day')

回答by Raphael Vitor

In angularjs moment="^1.3.0"

在 angularjs moment="^1.3.0"

moment('15-01-1979', 'DD-MM-YYYY').subtract(1,'days').format(); //14-01-1979
or
moment('15-01-1979', 'DD-MM-YYYY').add(1,'days').format(); //16-01-1979
``



回答by canmustu

var date = new Date();

var targetDate = moment(date).subtract(1, 'day').toDate(); // date object

Now, you can format how you wanna see this date or you can compare this date with another etc.

现在,您可以格式化您希望如何查看此日期,或者您可以将此日期与另一个日期进行比较等等。

toDate()function is the point.

toDate()函数是重点。

回答by lecstor

I think you have got it in that last attempt, you just need to grab the string.. in Chrome's console..

我想你已经在最后一次尝试中得到了它,你只需要在 Chrome 的控制台中获取字符串..

startdate = moment();
startdate.subtract(1, 'd');
startdate.format('DD-MM-YYYY');
"14-04-2015"

startdate = moment();
startdate.subtract(1, 'd');
myString = startdate.format('DD-MM-YYYY');
"14-04-2015"
myString
"14-04-2015"