如何从moment.js获取月份的整数值 [英] How to get the integer value of month from moment.js

查看:53
本文介绍了如何从moment.js获取月份的整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Moment.js 将日期添加到我正在制作的选项列表中,以便我可以使用这些日期来显示可用约会,例如,有人可以从选项列表中选择 2 月 3 日星期五,然后选择一个列表可用时间将在 2 月 3 日显示.

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.

我遇到的问题是我使用了一个在线调度 API,它采用从 1 开始的月份值,例如,一月是 01,二月是 02,等等.但是 moment.js 月份从 0 开始,并且只去最多 11,例如,一月是 0,二月是 1 等等.我需要能够将 moment.js 中的值转换为整数,以便我可以为每个值加 1 以解决差异.

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.

真正的问题是我尝试使用 parseInt(Month) 来获取 int 值以将其加一,但这没有用.这是我试图做到这一点的代码:

The real problem is I tried using parseInt(Month) to get the int value to add one to it, but that didn't work. 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 仍然只返回 1 月的值,有什么特殊的方法可以获取该月的 int 值吗?

GetMonth still only returns 1 for February, is there some special way I can get the int value from the month?

推荐答案

GetMonth = GetMonthint++;

是问题所在.++ 运算符返回原始值而不是递增的值.你应该这样做:

is the problem. The ++ operator returns the original value not the incremented value. You should do this:

GetMonth = GetMonthint + 1;

这篇关于如何从moment.js获取月份的整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆