-3

Say for example my Date (formerly NSDate) object was created as the 12 June 2016 at 14:23, how would I extract just that 14:23 from my Date object? Basically some thing like mydate.time.

2
  • Your link is helpful however it isn't in Swift 3. .CalendarUnitHour and .CalendarUnitMinute are not accepted anymore. Commented Sep 8, 2016 at 18:23
  • Your post is tagged as just Swift, not Swift 3.
    – Wyetro
    Commented Sep 8, 2016 at 18:24

1 Answer 1

0

Here's a handy extension so you use let time = Date().time

extension Date {
    var time: String {
        let calendar = Calendar.current
        let components = calendar.dateComponents([.hour, .minute], from: self)
        return "\(components.hour!):\(components.minute!)"
    }
}
5
  • Try now, changed to support swift 3
    – Callam
    Commented Sep 8, 2016 at 18:36
  • @Callam, as of Swift 3 you don't use the NS prefix.
    – Wyetro
    Commented Sep 8, 2016 at 18:38
  • @WMios yeah my bad, fixed it
    – Callam
    Commented Sep 8, 2016 at 18:39
  • @MartinR just tested it in swift 3 playground and it works
    – Callam
    Commented Sep 8, 2016 at 18:43
  • @MartinR yes, after I edited if that's what you're getting at
    – Callam
    Commented Sep 8, 2016 at 19:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.