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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:54:34+00:00 2026-06-09T21:54:34+00:00

I have few NSDate objects which contain values compliant to this format yyy-MM-dd’T’HH:mm:ss.SSS When

  • 0

I have few NSDate objects which contain values compliant to this format yyy-MM-dd'T'HH:mm:ss.SSS

When I try to convert to a different format such as MMM dd, yyyy HH:mm the formatter always returns nil.

However, if I hardcode the string value I get through the NSDate object , it is transformed to the new format without a problem.

I populate my NSDate object in the model using the setValue:forKey: method, and I feel the problem is here where the JSON library passes a string value. However, the debugging shows that the NSDate has contains the value returned via JSON.

Any idea what’s causing this strange behavior?

This is my conversion code:

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSString *createdDateStr = [format stringFromDate:modelObj.createdDate];
[format setDateFormat:@"MMM dd, yyyy HH:mm"];

NSDate *formattedDate = [format dateFromString:createdDateStr];
NSLog(@"========= REal Date %@",[format stringFromDate:formattedDate]);
  • 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-09T21:54:35+00:00Added an answer on June 9, 2026 at 9:54 pm

    EDIT: Rereading your question, I think you’ve got a conceptual misunderstanding aside from anything else:

    I have few NSDate objects which contains values compliant to the following format

    As far as I’m aware (I’m not an Objective-C programmer, admittedly) an NSDate object doesn’t know anything about formatting. It just represents an instant in time. It isn’t aware of whether it was parsed from text or created in some other way.

    It’s like an int – an int variable isn’t in decimal or hex, it’s just a number. It’s when you format and parse that you specify that. Likewise, you use NSDateFormatter to describe the format you want at the point of parsing and formatting.


    You’ve currently only got one NSDateFormatter – you’re setting the format to "yyyy-MM-dd'T'HH:mm:ss.SSS" then resetting it before you’ve called dateFromString. The code snippet you’ve given formats modelObj.createdDate into ISO-8601 format, but then tries to parse it using the “MMM dd, yyyy HH:mm” format.

    I suggest you have two separate NSDateFormatter objects:

    NSDateFormatter *isoFormat = [[NSDateFormatter alloc] init];
    [isoFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
    NSString *createdDateStr = [format stringFromDate:modelObj.createdDate];
    
    NSDateFormatter *shortFormat = [[NSDateFormatter alloc] init];
    [shortFormat setDateFormat:@"MMM dd, yyyy HH:mm"];
    
    NSDate *parsedDate = [isoFormat dateFromString:createdDateStr];
    NSLog(@"========= REal Date %@",[shortFormat stringFromDate:parsedDate]);
    

    Note that I’ve changed your formattedDate variable to parsedDate, as that’s what it really is – an NSDate which has been created by parsing text.

    As noted in comments, unless you actually need to format and parse like this, you shouldn’t. I included all of your original code just to show how you can format a date and then reparse it with the same NSDateFormatter. You’ll get the same result with just:

    NSDateFormatter *shortFormat = [[NSDateFormatter alloc] init];
    [shortFormat setDateFormat:@"MMM dd, yyyy HH:mm"];    
    NSLog(@"========= REal Date %@",[shortFormat stringFromDate:modelObj.createdDate]);
    

    In general, you should avoid unnecessary conversions. Convert from text to the more natural data type (NSDate in this case, but the same applies for numbers etc) and use that data type for as much of your code as possible, only converting to text at the last moment.

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

Sidebar

Related Questions

I have retrieved few dates from an array ,which where stored in string format
I have few graphic effects on image which i am able to run on
I have few question in this regard When you create an internet page, does
I have a few custom UIViewControllers in my app which need to know the
I have seen a few posts which suggest using something like the following inside
i have few variables like this: self.lamp_1 self.lamp_2 self.lamp_3 self.lamp_4 and now i want
I have few textviews and it looks like this now: ramdomName 10.20 - 11.30
I have few questions on this as selenium always need windowid to get control
I have few questions about this code: <?php $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); $iv =
We have few projects, which are compiling my makefile. These make file working fine

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.