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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:20:47+00:00 2026-05-23T08:20:47+00:00

I am using Date Formatter, getting the NSDateComponents. And then extracting the individual time

  • 0

I am using Date Formatter, getting the NSDateComponents. And then extracting the individual time components such as hour, minute..etc..
How can I extract these components as NSIntegers without losing the ‘0’ digits at the front of the number.

E.g is today was january 1 2010 5:05, it would print 1-1-2010-5:5
I want 01-01-2010-05:05

Any help would be appreciated

  • 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-23T08:20:48+00:00Added an answer on May 23, 2026 at 8:20 am

    There is no way you can keep 0 in the NSInteger. As such you can pick one of the approaches here –

    Avoid date components and reprint date in a different format using NSDateFormatter

    NSCalendar * gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    
    NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"MMMM d yyyy H:mm"];
    NSDate * theDate = [dateFormatter dateFromString:@"January 1 2010 5:05"];
    
    /* Using NSDateFormatter to get the desired format */
    [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];
    NSLog(@"%@", [dateFormatter stringFromDate:theDate]);
    

    From Date components

    NSInteger flags = (NSHourCalendarUnit|NSMinuteCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit);
    NSDateComponents * dateComponents = [gregorianCalendar components:flags
                                                             fromDate:theDate];
    
    NSInteger hour = [dateComponents hour];
    NSInteger minute = [dateComponents minute];
    NSInteger year = [dateComponents year];
    NSInteger month = [dateComponents month];
    NSInteger day = [dateComponents day];
    
    NSLog(@"%02ld-%02ld-%ld %02ld:%02ld", day, month, year, hour, minute);
    

    Using NSNumberFormatter (extending the previous block)

    NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    [numberFormatter setPaddingCharacter:@"0"];
    [numberFormatter setMinimumIntegerDigits:2];
    
    NSString * hourString = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:hour]];
    NSString * minuteString = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:minute]];
    NSString * yearString = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:year]];
    NSString * monthString = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:month]];
    NSString * dayString = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:day]];
    
    NSLog(@"%@-%@-%@ %@:%@", dayString, monthString, yearString, hourString, minuteString);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any reason behind using date(January 1st, 1970) as default standard for time
i have one problem when i sort the NSMutablearray using date and time wise.
hi every one I'm using this code, through this I'm getting wrong time,i need
I am using maskedEditExtender control for getting date.. Code inside MaskedEditExtender is cc1:MaskedEditExtender ID=MaskedEditExtender1
I am getting the current time using these code. NSTimeInterval timeInterval = [moviePlayer currentPlaybackTime];
I swear I'm using the correct date format but I keep getting a parse
I am using a date formatter string in order to get the current date
How can I compare the dates only, not the time. I am using NSDateFormatter
I coded something using Date statement in Access VBA. It was working fine until
I am trying to output dates in the Italian format using date() as follows:

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.