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 6471659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:15:01+00:00 2026-05-25T06:15:01+00:00

I have created an NSDate category that would give me a last sunday date.

  • 0

I have created an NSDate category that would give me a “last sunday” date.

+(NSDate *)sunday{
  // I need 2 dates yesterday and today
  NSCalendar *gregorian   = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
  NSDate *now = [NSDate date];
  NSLog(@"Current date: %@", now);
  NSDateComponents *components = [gregorian components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit ) fromDate:now];
  [components setWeekday:1]; //Monday 
  [components setHour:0]; //8a.m.
  [components setMinute:0];
  [components setSecond:0];
  NSLog(@"Sunday: %@", [gregorian dateFromComponents:components]);
  return [gregorian dateFromComponents:components]; 
}

This give me a couple different dates:

IOS 4.2

Sunday: 0001-08-28 05:50:36 +0000

IOS 4.1

Sunday: 0001-09-04 05:50:36 GMT

Notice
That IOS 4.1 give me a future date, whereas IOS 4.2 gives me a previous date, which is what I want.

I understand that the year is 0001 is shown, but the year is not used.

I have not yet found where others are having this same issue, so perhaps I’m doing something wrong. But, I’m not sure what that is. Has anyone seen this before?

EDIT
Here is my working code:

+(NSDate *)sunday{
  NSCalendar *gregorian   = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
  NSDate *now = [NSDate date];
  NSDateComponents *components = [gregorian components:(NSWeekCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit ) fromDate:now];
  [components setWeekday:[gregorian firstWeekday]]; //Sunday
  [components setHour:0]; 
  [components setMinute:0];
  [components setSecond:0];
  return [gregorian dateFromComponents:components];
}

Thanks!!

  • 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-05-25T06:15:01+00:00Added an answer on May 25, 2026 at 6:15 am

    You should probably reconsider your assertion that the year is not used. Sure, 2011-09-04 and (by the Julian calendar used by NSGregorianCalendar for dates before 1582-10-15) 0001-09-04 are both Sundays so it seems like it doesn’t matter. But next year it won’t match up.

    Your problem with the differing results on the two devices is most likely that your devices are set to different regions, with different rules about which day is the first day of the week or which week is the first of the year. You can check the firstWeekday property on your NSCalendar object to determine the first, and minimumDaysInFirstWeek will help with the latter.

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

Sidebar

Related Questions

I have created a custom class for my dates and need move dates forward
I have some reviews with the date that has been created, so I want
I have an NSDate object created by NSDate *date = [[NSDate alloc] init]; Later,
I have created a category for NSDate which can create a string representation of
I have created a custom class which show time date formatter and I need
I have created some JQuery that will expand a div 'popup' on hover and
I have created an android application that calls (using kSOAP library) a SOAP based
I have created a function that shows/hides different messages according to a combination of
I have created a dialog and got a problem that only part of the
I have created a gaming application that has only one window. Application is created

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.