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

  • Home
  • SEARCH
  • 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 8552107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:20:55+00:00 2026-06-11T14:20:55+00:00

I have some code I use to sort calendar dates that looks like this:

  • 0

I have some code I use to sort calendar dates that looks like this:

#if !(TARGET_IPHONE_SIMULATOR)
    NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm dd MMM yyyy" options:0
                                                              locale:[NSLocale currentLocale]];
    [fmt setDateFormat:formatString];
#else
    [fmt setDateFormat:@"HH:mm dd MMM yyyy"];
#endif

If I run it in the simulator all is ok. If I run it on the device I get this sarcastic debug message.

2012-09-19 22:40:13.972 APPNAME [4923:907] * -[__NSCFCalendar
components:fromDate:]: date cannot be nil

I mean really, what do you
think that operation is supposed to mean with a nil date?

An exception has been avoided for now.

A few of these errors are going to be
reported with this complaint, then further violations will simply
silently do whatever random thing results from the nil. Here is the
backtrace where this occurred this time (some frames may be missing
due to compiler optimizations):

You have to laugh at it but I’m not sure what is wrong with my dateFormatFromTemplate: code. Any help would be appreciated.

Running Xcode V 4.5 btw


UPDATE:

Backtrace:

0 CoreFoundation 0x39ff0e55 + 84
1
APPNAME 0x00040be1
-[MeetingsViewController dateAtBeginningOfDayForDate:] + 140

So I guess things are going bad in my dateAtBeginningOfDayForDate method. Which looks like this:

/*
 Break down a given NSDate to its bare components for sorting
 */
- (NSDate *)dateAtBeginningOfDayForDate:(NSDate *)inputDate
{
    // Use the user's current calendar and time zone
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    [calendar setTimeZone:timeZone];

    // Selectively convert the date components (year, month, day) of the input date
    NSDateComponents *dateComps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:inputDate];

    // Set the time components manually
    [dateComps setHour:0];
    [dateComps setMinute:0];
    [dateComps setSecond:0];

    // Convert back
    NSDate *beginningOfDay = [calendar dateFromComponents:dateComps];
    return beginningOfDay;
}

I use this method to break down a given NSDate to its’ core components so that I can sort them all more efficiently. However, my app has to be international which is the reason behind using NSLocale to formate the date output. From what it seems I will need to alter my dateAtBeginningOfDayForDate to work internationally as well.

  • 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-11T14:20:56+00:00Added an answer on June 11, 2026 at 2:20 pm

    Much thanks to nneonneo for helping me figure this out.

    My issue is that my app has to work internationally so I needed dates to display with respect to the user location. I thought that I would just create a NSDateFormatter to the users locale then pass in a string date like so:

     NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm dd MMM yyyy" options:0
                                                                  locale:[NSLocale currentLocale]];
    date = [fmt dateFromString:[NSString stringWithFormat:@"%@ %@", obj.meetingTime, obj.meetingDate]];
    

    However, the problem is that I was localization the date format before I passed in the string date. In my case NSDateFormatter looked like this after localizing.

    MMM dd, yyyy, HH:mm
    

    Which I the format after localization could look many different ways after localizing.

    So passing in a string with a format of HH:mm dd MMM yyyy was causing it to return nil.

    Which is obvious now -.-

    So instead of localizing when I create the date, I create a the dates with a standard format of HH:mm dd MMM yyyy. Then when displaying, I format the NSDate to the users respective locale.

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

Sidebar

Related Questions

I have some code that does something like this (Irrelevant bits snipped): void foo(Bitmap
I have some code that I use in both my Worker Role and Web
I have some code that makes heavy use of a thread pool, which I
I use this bit of code to feed some data i have parsed from
I have some code that is supposed to return an NSString. Instead it is
Occasionally I'll have some WPF C# code that I'd like to write in a
I have some thing like this code: map<int, string> m; m[1] = a; m[2]
We are migrating some code to use Entity Framework and have a query that
I have some Scala code that makes fairly heavy use of generics, and I
I have some code where I use a thread static object in C#. [ThreadStatic]

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.