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 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'm having trouble getting a decent query time out of a large MySQL table,
I'm having trouble getting the following to work in SQL Server 2k, but it
I am having trouble getting my ASP.NET application to start an application. For example
I'm having some trouble getting log4net to work from ASP.NET 3.5. This is the
I've installed CruiseControl.net for the first time and I'm having trouble getting my solution
From PHP, I'm having trouble getting the browser to delete a cookie. To chase
I'm having a lot of trouble with Ruby after coming back to it from
I'm having trouble getting a rotary encoder to work properly with AVR micro controllers.
I've been having trouble getting my ASP.NET application to automatically log users into the
I'm having trouble getting the right number of elements in the ArrayList alt in

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.