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 badges3 Answers
Reset to default 4This 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");