Javascript - 获取上个月的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26488043/
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-22 22:57:51  来源:igfitidea点击:

Javascript - Get Previous Months Date

javascriptjqueryangularjsangularjs-scope

提问by Oam Psy

If i have a variable that returns a date, in the format of dd MMM yyyy, so 28 Aug 2014, how can i get the date of the previous month.

如果我有一个返回日期的变量,格式为 dd MMM yyyy,即 2014 年 8 月 28 日,我如何获取上个月的日期。

I can modify the month via:

我可以通过以下方式修改月份:

$scope.DateMapping = function (months) {

    var myVariable = "28 Aug 2014"
    var makeDate = new Date(myVariable);
    prev = new Date(makeDate.getFullYear(), makeDate.getMonth()+1, 1);

});

Essentially, this is adding one to the Month.. But how can i account for years, so if the current date is 12 Dec 2014, the previous would be 12 Jan 2013?

本质上,这是给月份加一。但是我如何计算年份,所以如果当前日期是 2014 年 12 月 12 日,那么之前的日期是 2013 年 1 月 12 日?

My application is using AngularJS can make use of filters.

我的应用程序使用 AngularJS 可以利用过滤器。

UPDATE:

更新:

    var myVariable = "28 Aug 2014"
    var makeDate = new Date(myVariable);
    var prev = new Date(makeDate.getFullYear(), makeDate.getMonth()+1, makeDate.getMonth());

    console.log(myVariable)
    console.log(makeDate)
    console.log(prev)

Output:

    28 Aug 2014
    Thu Aug 28 2014 00:00:00 GMT+0100 (GMT Standard Time)
    Mon Sep 01 2014 00:00:00 GMT+0100 (GMT Standard Time)

How comes although the month has incremented, the day is showing as 01 instead of 28?

为什么虽然月份增加了,但日期显示为 01 而不是 28?

采纳答案by Dmitry

var myVariable = "28 Aug 2014"
var makeDate = new Date(myVariable);
makeDate = new Date(makeDate.setMonth(makeDate.getMonth() - 1));

Update:

更新:

A shorter version:

一个较短的版本:

var myVariable = "28 Aug 2014"
var makeDate = new Date(myVariable);

console.log('Original date: ', makeDate.toString());

makeDate.setMonth(makeDate.getMonth() - 1);

console.log('After subtracting a month: ', makeDate.toString());

Update 2:

更新 2:

If you don't want to deal with corner cases just use moment.js. Native JavaScript API for Date is bad.

如果您不想处理极端情况,请使用 moment.js。Date 的原生 JavaScript API 很糟糕。

回答by RaYell

Just subtract the number of months from the month parameter and don't worry if the value is going to be negative. It will be handled correctly.

只需从月份参数中减去月数,不要担心该值是否为负数。它将被正确处理。

new Date(2014, 0, 1) // 1st Jan 2014
new Date(2014, -1, 1) // 1st Dec 2013

回答by Leon Revill

For all date related activities I recommend using the moment.js library and as you are using AngularJS there is a library for this: https://github.com/urish/angular-moment.

对于所有与日期相关的活动,我建议使用 moment.js 库,当您使用 AngularJS 时,有一个用于此的库:https: //github.com/urish/angular-moment

Take a look at the moment.js documentation and you will see it makes it much easier to manipulate dates in JavaScript: http://momentjs.com/docs/#/get-set/month/

查看 moment.js 文档,您会发现它使在 JavaScript 中操作日期变得更加容易:http: //momentjs.com/docs/#/get-set/month/

You will easily be able to solve the problem you are facing, for example:

您将能够轻松解决您面临的问题,例如:

var now = moment('01/01/2014').subtract(1, 'months').format('DD/MM/YYYY');

nowwould then equal 01/12/2013.

现在将等于 01/12/2013。

Hope this helps!

希望这可以帮助!

回答by Davide Pastore

You could use moment.js subtract functionfor this.

您可以为此使用moment.js 减法函数

var day = moment("2014-08-28");
day.subtract(1, 'months');

回答by Showtek

There is my way to get previous months.

我有办法获得前几个月。

const formatter = new Intl.DateTimeFormat("default", {
  month: "long"
});
const date = new Date();

let number = 1;
let prevMonth = formatter.format(
  new Date(date.getFullYear(), date.getMonth() - `${number}`)
);