最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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");
发布评论

评论列表(0)

  1. 暂无评论