Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8207303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:57:23+00:00 2026-06-07T08:57:23+00:00

I am using this method to convert a month and year to a date

  • 0

I am using this method to convert a month and year to a date which equals the last day in the month of the year given.

+ (NSDate*)endOfMonthDateForMonth:(int)month year:(int)year
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps.year = year;
    comps.month = month;

    NSDate *monthYearDate = [calendar dateFromComponents:comps];

    // daysRange.length will contain the number of the last day of the endMonth:
    NSRange daysRange = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:monthYearDate];
    comps.day = daysRange.length;
    comps.hour = 0;
    comps.minute = 0;
    comps.second = 0;
    [comps setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    NSDate *endDate = [calendar dateFromComponents:comps];
    return endDate;
}

I want the date to have a time component of 00:00:00, thats why I have set the time zone to GMT 0 and the date components for minutes, hours and seconds to 0. The date returned from the method is correct and has a time component from 00:00:00.

This is how I save the date to Core Data:

NSDate *endDate = [IBEstPeriod endOfMonthDateForMonth:endMonth year:endCalYear];
[annualPeriod setEndDate:endDate];

After retrieving the data and NSLogging it to the debugger console, I get dates like 2008-12-30 23:00:00 +0000 with a time component != 0.

Why did the component change now?
Shouldn’t it stay at 00:00:00?

What did I code wrong here?

Thank you!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:57:26+00:00Added an answer on June 7, 2026 at 8:57 am

    You just need to set the calendar time zone after creating calendar.

    Add this as the second line of your function:

    calendar.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    

    However, an easier way to do it would be this:

    - (NSDate*)endOfMonthDateForMonth:(int)month year:(int)year
    {
        NSCalendar *calendar    = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        calendar.timeZone       = [NSTimeZone timeZoneWithName:@"UTC"];
    
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        comps.year              = year;
        comps.month             = month+1;
        comps.day               = 0;
    
        NSDate *monthYearDate   = [calendar dateFromComponents:comps];
        return monthYearDate;
    }
    

    Results:

    NSLog(@"%@",[self endOfMonthDateForMonth:12 year:2010]);
    // Output: 2012-07-10 12:30:05.999 Testing App[16310:fb03] 2010-12-31 00:00:00 +0000
    

    This works by setting the date to the “0th” day of next month, which is the same as the last day of this month. (This does the same thing as subtracting one day from the first day of next month.) Note that this works because comps are allowed to “overflow” (or in this case “underflow”) and dateFromComponents: does the math automatically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering why the method String.valueOf(int i) exists ? I am using this
I have been using this method to convert a UIView into UIImage . i.e.
I want to convert decimal number in binary number. I'm using this method: -
when using this method public List<Field> getFieldWithoutId(List<Integer> idSections) throws Exception { try { Connection
I am using this method on a button click. My app is not yet
I'm using this method to install an application, but it doesn't put the applications
I deployed an application using this method and it worked very good. However, there
I find myself using this method a ton to perform actions based on a
I'm currently using this method in calling batch files in vb.net: Private Sub Button3_Click(ByVal
Should I always be using this method when rendering? Does it slow down much

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.