简体   繁体   中英

Flex: How to get the number of Days in a particular month in Flex?

I am having a problem with flex. How can I get the number of Days in a particular month in Flex?

Thanks

Use the Date object , setting the day to 0, getDate will return the last day in the month which is also the day count; you have also to give the year you want to check, because you know february can have 29 days.

function getDayCount(year:int, month:int):int{
 var d:Date=new Date(year, month, 0);
 return d.getDate();
}

trace(getDayCount(2012,2));

Create a new date object by specifying the year, desired month + 1, and a day of 0. This will create a date object for the last day of the desired month. Then call getDate() on the object to return the last day.

Note that months are zero based in Flex, so Jan = 0, Feb = 1, and so on. Therefore, if you want to know what the last day in Feb was for 2012 you would do the following:

var FEB:int = 1;
var date:Date = new Date(2012, FEB + 1, 0);
var lastDayInFeb:Number = date.getDate();

Here is a more complete example with a couple of non-unit tests and a reusable static method for returning the last day of a month.

The example above doesn't work for January! new Date(2011,0,0) returns 12/31/2010
new Date(2011, 1, 0) returns 01/31/2011
new Date(2011, 1, 1) returns 02/01/2011

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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