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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:19:10+00:00 2026-05-19T09:19:10+00:00

I am having trouble getting the time for today from a string. I am

  • 0

I am having trouble getting the time for today from a string. I am reading in a time from a UITextField, but the date is coming out as the start of the epoch

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH-mm"];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setLenient:YES];

NSLog(@"Format: %@",[formatter dateFormat]);    
NSDate *now = [formatter dateFromString:[textbox text]];
NSLog(@"Time from textfield: %@",now]);

This gives the following date from the log code.

2011-01-19 18:56:28.193 MyApp[8284:207] The time for now: 1970-01-01 06:00:00 GMT
  • 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-19T09:19:10+00:00Added an answer on May 19, 2026 at 9:19 am

    Setting the dateStyle and timeStyle for the formatter will override the dateFormat that you have set manually. The following should work as expected:

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH-mm"];
    [formatter setLenient:YES];
    
    NSLog(@"Format: %@",[formatter dateFormat]);
    
    [formatter release];
    

    The resulting date object will be close to the start of the epoch because you are not providing a year, month and day. If you want the time in the textfield to represent a time on today’s date, you will have to add in some extra code:

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH-mm"];
    [formatter setLenient:YES];
    NSDate *time = [formatter dateFromString:[textbox text]];
    [formatter release];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:NSYearCalendarUnit |
                                                   NSMonthCalendarUnit |
                                                   NSDayCalendarUnit |
                                                   NSHourCalendarUnit |
                                                   NSMinuteCalendarUnit
                                          fromDate:[NSDate date]];
    NSDateComponents *timeComps = [calendar components:NSHourCalendarUnit |
                                                       NSMinuteCalendarUnit
                                              fromDate:time];
    
    [comps setHour:[timeComps hour]];
    [comps setMinute:[timeComps minute]];
    NSDate *date = [calendar dateFromComponents:comps];
    NSLog(@"Date from textfield: %@",date);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've used jScrollPane before but I'm having some trouble getting it right this time.
I'm having trouble getting jQuery to allow only one content-DIV to be visible at
I'm having trouble getting Selenium to see sites hosted on a virtual machine. The
I'm having trouble getting a short MP3 file to play in a very small
This is for GNU-Prolog I'm having trouble getting a certain predicate to work. Its
I am trying to learn JDO (and at the same time its GAE and
Our shop is set up with a Team Foundation 2005 server but all development
I am new to AES encryption but trying to build a solution which: Accepts
I've create a style for ListItems that I want to use across all ListBoxes

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.