Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use an <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html" rel="noreferrer">NSDateFormatter</a> to convert <code>string1</code> into an <code>NSDate</code>, then get the required <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/Reference/Reference.html" rel="noreferrer">NSDateComponents</a>:</p> <p><strong>Obj-C</strong>:</p> <pre><code>NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"&lt;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]; </code></pre> <p><strong>Swift 1 and 2</strong>:</p> <pre><code>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 </code></pre> <p><strong>Swift 3</strong>:</p> <pre><code>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 </code></pre> <p>More about the <a href="http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns" rel="noreferrer">dateformat</a> is on the official unicode site</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload