javascript - How to get previous month and expected format from month picker using moment JS - Stack Overflow

I am using jquery.ui.monthpicker library. For month picker I am getting date like 072017. From this da

I am using jquery.ui.monthpicker library. For month picker I am getting date like 07/2017. From this date string I need to calculate previous month and formatted like 1707 using moment js library.

any help would be appreciated.

I am using jquery.ui.monthpicker library. For month picker I am getting date like 07/2017. From this date string I need to calculate previous month and formatted like 1707 using moment js library.

any help would be appreciated.

Share Improve this question asked Oct 5, 2017 at 7:38 Tushar GhoshTushar Ghosh 1,0221 gold badge13 silver badges18 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

This code may solve your problem.

moment("07/2017", "MM/YYYY").subtract(1, 'months').format('YYMM');

DEMO at https://jsfiddle/nffswx75/

    var dt = "07/2017";

    alert(moment(dt,"MM/YYYYY").format('YYMM'));

    alert(moment(dt,"MM/YYYYY").add(-1, 'months').format('YYMM'));

alert(moment(dt,"MM/YYYYY").subtract(1, 'months').format('YYMM'));

You can let moment create a date object from a string by telling it what format your date is in.

let dateString: string = "07/2017";
var date = moment(dateString, "MM/YYYY").subtract(1, 'month').format("YYMM");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745595608a4635093.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信