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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:11:42+00:00 2026-05-15T01:11:42+00:00

In my application I need to get the hour and minute separately: NSString *currentHour=[string1

  • 0

In my application I need to get the hour and minute separately:

NSString *currentHour=[string1 substringWithRange: NSMakeRange(0,2)];
        int currentHourInNumber=[currentHour intValue];

Consider string1 contains 11:59:13 AM which is coming from datepicker.

Here if I use above code, it’s okay to get hour if it’s greater than 9. Else I need to change NSMakeRange(0,1) to get hour between 1 to 9.

Are there any methods to get the hour, minutes, etc?

Please provide me sample code.

  • 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-15T01:11:43+00:00Added an answer on May 15, 2026 at 1:11 am

    Use an NSDateFormatter to convert string1 into an NSDate, then get the required NSDateComponents:

    Obj-C:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"<your date format goes here"];
    NSDate *date = [dateFormatter dateFromString:string1];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
    NSInteger hour = [components hour];
    NSInteger minute = [components minute];
    

    Swift 1 and 2:

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "Your date Format"
    let date = dateFormatter.dateFromString(string1)
    let calendar = NSCalendar.currentCalendar()
    let comp = calendar.components([.Hour, .Minute], fromDate: date)
    let hour = comp.hour
    let minute = comp.minute
    

    Swift 3:

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "Your date Format"
    let date = dateFormatter.date(from: string1)
    let calendar = Calendar.current
    let comp = calendar.dateComponents([.hour, .minute], from: date)
    let hour = comp.hour
    let minute = comp.minute
    

    More about the dateformat is on the official unicode site

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get the spring application context from a non bean object. In
I need to get all dlls in my application root directory. What is the
I'm working on a P2P application, and I need to get it to communicate
I'm looking to write a Twitter application, and I need to get real time
I'm using curlpp in an application and need to get the URL I was
I am creating a java application and I need to get the user PINs
I need to get the host name currently running the application. Any idea?
I need to get RoutData by given URL string in ASP.NET MVC application. I've
I need to get basic information about the computer's processor in a WPF application
my application processes strings with commandlines. I need to get the string to the

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.