简体   繁体   English

如何从moment.js获取月份的整数值

[英]How to get the integer value of month from moment.js

I am using Moment.js for adding dates to an option list I am making, so that I can use those dates to show available appointments, for example, someone can select Friday, February 3rd from the option list and then a list of available times will show up for February 3rd. 我正在使用Moment.js将日期添加到正在创建的选项列表中,以便可以使用这些日期显示可用约会,例如,某人可以从选项列表中选择2月3日,星期五,然后选择可用时间列表将于2月3日出现。

What I am having trouble with is I am using an online scheduling api that takes month values that start at 1, for example, January is 01, February is 02, etc. but moment.js months start at 0 and only go up to 11, for example, January is 0, February is 1, etc. I need to be able to convert the values from moment.js into an integer so I can add 1 to each to account for the difference. 我遇到的麻烦是我正在使用一个在线调度API,该API接受以1开头的月值,例如,一月是01,二月是02,依此类推,但是moment.js的月从0开始,直到11。 ,例如,一月为0,二月为1,依此类推。我需要能够将moment.js中的值转换为整数,因此我可以为每个值加1以解决差异。

The real problem is I tried using parseInt(Month) to get the int value to add one to it, but that didn't work. 真正的问题是我尝试使用parseInt(Month)来获取int值并将其添加一个,但这没有用。 Here is my code attempting to do just that: 这是我的代码试图做到这一点:

var d = moment(),  
Month = d.month();

var GetMonthint = parseInt(Month),
    GetMonth = GetMonthint++;

        GetAppointmentDates(GetMonth, Year);

GetMonth still only returns 1 for February, is there some special way I can get the int value from the month? GetMonth仍然只返回2月份的1,是否有一些特殊的方法可以从月份中获取int值?

The line 线

GetMonth = GetMonthint++;

is the problem. 是问题。 The ++ operator returns the original value not the incremented value. ++运算符返回原始值,而不是增量值。 You should do this: 你应该做这个:

GetMonth = GetMonthint + 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM