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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:25:48+00:00 2026-05-28T19:25:48+00:00

I’m having an issue converting minutes since midnight to a correct time. It works

  • 0

I’m having an issue converting minutes since midnight to a correct time. It works fine until i reach noon. At which point the time becomes 0:45AM and anything after noon is blank. Am I missing something in my format?

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"h:m"];
    NSLog(@"Minutes %i",minutes);
    NSString *time = [NSString stringWithFormat:@"%i:%i", (minutes / 60), (minutes % 60)];
    NSLog(@"time: %@",time);
    NSDate *formatTime = [dateFormatter dateFromString:time];
    NSLog(@"formatTime %@",formatTime);
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];
    [dateFormatter setDateFormat:@"H:mma"];
    NSString *newTime = [dateFormatter stringFromDate:formatTime];
    NSLog(@"NewTime: %@",newTime);
    dateFormatter = nil;

Log

2012-01-25 15:12:23.194 MyApp[43875:f803] Minutes 480
2012-01-25 15:12:23.194 MyApp[43875:f803] time: 8:0
2012-01-25 15:12:23.195 MyApp[43875:f803] formatTime 1970-01-01 15:00:00 +0000
2012-01-25 15:12:23.196 MyApp[43875:f803] NewTime: 8:00AM
2012-01-25 15:12:23.197 MyApp[43875:f803] Minutes 495
2012-01-25 15:12:23.197 MyApp[43875:f803] time: 8:15
2012-01-25 15:12:23.198 MyApp[43875:f803] formatTime 1970-01-01 15:15:00 +0000
2012-01-25 15:12:23.199 MyApp[43875:f803] NewTime: 8:15AM
2012-01-25 15:12:23.200 MyApp[43875:f803] Minutes 765
2012-01-25 15:12:23.200 MyApp[43875:f803] time: 12:45
2012-01-25 15:12:23.201 MyApp[43875:f803] formatTime 1970-01-01 07:45:00 +0000
2012-01-25 15:12:23.201 MyApp[43875:f803] NewTime: 0:45AM
2012-01-25 15:12:23.203 MyApp[43875:f803] Minutes 480
2012-01-25 15:12:23.203 MyApp[43875:f803] time: 8:0
2012-01-25 15:12:23.204 MyApp[43875:f803] formatTime 1970-01-01 15:00:00 +0000
2012-01-25 15:12:23.204 MyApp[43875:f803] NewTime: 8:00AM
2012-01-25 15:12:23.205 MyApp[43875:f803] Minutes 495
2012-01-25 15:12:23.206 MyApp[43875:f803] time: 8:15
2012-01-25 15:12:23.206 MyApp[43875:f803] formatTime 1970-01-01 15:15:00 +0000
2012-01-25 15:12:23.207 MyApp[43875:f803] NewTime: 8:15AM
2012-01-25 15:12:23.208 MyApp[43875:f803] Minutes 555
2012-01-25 15:12:23.209 MyApp[43875:f803] time: 9:15
2012-01-25 15:12:23.209 MyApp[43875:f803] formatTime 1970-01-01 16:15:00 +0000
2012-01-25 15:12:23.210 MyApp[43875:f803] NewTime: 9:15AM
2012-01-25 15:12:23.211 MyApp[43875:f803] Minutes 630
2012-01-25 15:12:23.211 MyApp[43875:f803] time: 10:30
2012-01-25 15:12:23.212 MyApp[43875:f803] formatTime 1970-01-01 17:30:00 +0000
2012-01-25 15:12:23.212 MyApp[43875:f803] NewTime: 10:30AM
2012-01-25 15:12:23.214 MyApp[43875:f803] Minutes 690
2012-01-25 15:12:23.214 MyApp[43875:f803] time: 11:30
2012-01-25 15:12:23.215 MyApp[43875:f803] formatTime 1970-01-01 18:30:00 +0000
2012-01-25 15:12:23.215 MyApp[43875:f803] NewTime: 11:30AM
2012-01-25 15:12:23.217 MyApp[43875:f803] Minutes 765
2012-01-25 15:12:23.217 MyApp[43875:f803] time: 12:45
2012-01-25 15:12:23.217 MyApp[43875:f803] formatTime 1970-01-01 07:45:00 +0000
2012-01-25 15:12:23.218 MyApp[43875:f803] NewTime: 0:45AM
2012-01-25 15:12:32.506 MyApp[43875:f803] Minutes 840
2012-01-25 15:12:32.507 MyApp[43875:f803] time: 14:0
2012-01-25 15:12:32.507 MyApp[43875:f803] formatTime (null)
2012-01-25 15:12:32.508 MyApp[43875:f803] NewTime: (null)

EDIT:
I am using Munid’s Edit 2 below and it works well except it seems to be subtracting 7 hours from newDate when I run it through the dateFormatter

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    //dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];
    [dateFormatter setDateFormat:@"hh:mm a"];
    NSDate *midnight = [NSDate dateWithTimeIntervalSince1970:0];
    NSLog(@"midnight %@",midnight);
    NSDate *newDate = [midnight dateByAddingTimeInterval:minutes*60];
    NSLog(@"new date %@", newDate);
    //[dateFormatter setDateFormat:@"hh:mma"];
    NSString *newTime = [dateFormatter stringFromDate:newDate];
    NSLog(@"Time: %@", newTime);
    dateFormatter = nil;

will log

2012-01-25 16:15:03.418 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.418 MyApp[44721:f803] new date 1970-01-01 08:00:00 +0000
2012-01-25 16:15:03.419 MyApp[44721:f803] Time: 01:00 AM
2012-01-25 16:15:03.420 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.420 MyApp[44721:f803] new date 1970-01-01 08:15:00 +0000
2012-01-25 16:15:03.420 MyApp[44721:f803] Time: 01:15 AM
2012-01-25 16:15:03.421 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.422 MyApp[44721:f803] new date 1970-01-01 12:45:00 +0000
2012-01-25 16:15:03.422 MyApp[44721:f803] Time: 05:45 AM
2012-01-25 16:15:03.423 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.424 MyApp[44721:f803] new date 1970-01-01 08:00:00 +0000
2012-01-25 16:15:03.424 MyApp[44721:f803] Time: 01:00 AM
2012-01-25 16:15:03.425 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.425 MyApp[44721:f803] new date 1970-01-01 08:15:00 +0000
2012-01-25 16:15:03.426 MyApp[44721:f803] Time: 01:15 AM
2012-01-25 16:15:03.427 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.427 MyApp[44721:f803] new date 1970-01-01 09:15:00 +0000
2012-01-25 16:15:03.427 MyApp[44721:f803] Time: 02:15 AM
2012-01-25 16:15:03.428 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.429 MyApp[44721:f803] new date 1970-01-01 09:30:00 +0000
2012-01-25 16:15:03.429 MyApp[44721:f803] Time: 02:30 AM
2012-01-25 16:15:03.430 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.431 MyApp[44721:f803] new date 1970-01-01 10:30:00 +0000
2012-01-25 16:15:03.431 MyApp[44721:f803] Time: 03:30 AM
2012-01-25 16:15:03.432 MyApp[44721:f803] midnight 1970-01-01 00:00:00 +0000
2012-01-25 16:15:03.432 MyApp[44721:f803] new date 1970-01-01 11:30:00 +0000
2012-01-25 16:15:03.433 MyApp[44721:f803] Time: 04:30 AM
  • 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-28T19:25:49+00:00Added an answer on May 28, 2026 at 7:25 pm

    First, maybe your method with format strings is a little bit too involved. Don’t forget, that the NSLog output also needs to be formatted, otherwise you will get a different locale and time zone, so these values are not reliable for checking if your logic is right.

    The simple solution is to use the fact that NSTimeInterval is in seconds. Thus:

    NSDate *midnight; // the date you want, at 0:00 am
    NSDate *newDate = [midnight dateByAddingTimeInterval:minutes*60];
    

    EDIT:

    Also, I checked your code – you can fix it by changing the format strings. The problem was that the date formatter got confused with the minute having 1 or 2 digits.

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    for (NSInteger minutes = 495; minutes < 1000; minutes +=60) {
        [dateFormatter setDateFormat:@"hh:mm"];
        NSLog(@"Minutes %2i",minutes);
        NSString *time = [NSString stringWithFormat:@"%2i:%2i", (minutes / 60), (minutes % 60)];
        NSLog(@"time: %@",time);
        NSDate *formatTime = [dateFormatter dateFromString:time];
        NSLog(@"formatTime %@",formatTime);
        dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];
        [dateFormatter setDateFormat:@"HH:mm a"];
        NSString *newTime = [dateFormatter stringFromDate:formatTime];
        NSLog(@"NewTime: %@",newTime);
    }
    dateFormatter = nil;        
    

    EDIT2:

    Expanding on the code above as requested:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"hh:mm a"];
    NSDate *midnight = [NSDate dateWithTimeIntervalSince1970:0];
    for (NSInteger minutes = 495; minutes < 1000; minutes +=60) {
        NSDate *newDate = [midnight dateByAddingTimeInterval:minutes*60];
        NSLog(@"newDate: %@", [dateFormatter stringFromDate:newDate]);
    }
    dateFormatter = nil;        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.