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

The Archive Base Latest Questions

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

How can I parse a string that represents a date and/or time using iPhone

  • 0

How can I parse a string that represents a date and/or time using iPhone SDK if I do not know the format? I am building an application that will allow a range of possible dates and times to be used, and I don’t want it to fail because the format was “01/01/2001” instead of “01-01-2001”, etc. Time may or may not be included.

Is there any way to do this? I have tried using NSDateFormatter with date and time styles set to “NoStyle”, and setLenient = YES, but it still fails to parse the simplest of strings: “1/22/2009 12:00:00 PM”

I have no way of knowing the format in advance, I need a method that will heuristically determine the format.

I am coming from a .NET background, where this is as simple as new DateTime(string_in_any_format); but I have no idea how to do this in Obj-C.

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

    Unfortunately, NSDateFormatter on the iPhone isn’t that smart. You need to give it a little guidance. When you set both the date and time styles to NoStyle it expects to see neither a date nor time. You’ll need at least one of them set to some other style. You can do something like the following and it should work fine (it does for me).

    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterLongStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setLenient:YES];
    
    NSDate * date = [formatter dateFromString:@"01-01/2001"];
    
    NSLog(@"%@", date);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi how i can parse this json string: {Error:true, data:[not available,somethinghere]} but that string
I have to parse a String that can assume hex values or other non-hex
Does anybody know a Perl library that can parse XML documents and enables me
Lets say I have a string that represents a date that looks like this:
I want to parse a search string similar to that provided by Gmail using
Is there a single regular expression that can parse a string (in Python and/or
I'm looking for a JavaScript library which can parse and format date values and
I have a String 20120119 which represents a date in the format 'YYYYMMDD' .
I can parse the simple JSON string using the following code HttpClient client =
I need a PHP Regex that can parse .strings files. In particular, it needs

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.