最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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");
Post a comment

comment list (0)

  1. No comments so far