73

How would I get the hour of the day in Swift. I have tried NSCalendar and NSDateComponents, but I'm afraid I'm just starting with Swift.

Suraj Sonawane
  • 2,044
  • 1
  • 14
  • 24
Samuel Gómez
  • 877
  • 1
  • 6
  • 7

7 Answers7

193

Swift 5.0 / 4.0 / 3.0

let hour = Calendar.current.component(.hour, from: Date())

 

Or, if you're interested in 12 hour AM/PM date format, then use NSDateFormatter

let formatter = DateFormatter()
formatter.dateFormat = "hh a" // "a" prints "pm" or "am"
let hourString = formatter.string(from: Date()) // "12 AM"

If you want minutes, seconds and others, do as following

let date = Date() // save date, so all components use the same date
let calendar = Calendar.current // or e.g. Calendar(identifier: .persian)

let hour = calendar.component(.hour, from: date)
let minute = calendar.component(.minute, from: date)
let second = calendar.component(.second, from: date)

Check out available components on Apple docs:

.era, .year, .month, .day, .hour, .minute, .second,
.weekday, .weekdayOrdinal, .quarter, weekOfMonth, .weekOfYear,
.yearForWeekOfYear, .nanosecond, .calendar, .timezone

Swift 2.0

let hour = NSCalendar.currentCalendar().component(.Hour, fromDate: NSDate())

Swift 1.0

let hour = NSCalendar.currentCalendar().component(.CalendarUnitHour, fromDate: NSDate())
average Joe
  • 4,377
  • 2
  • 25
  • 23
  • 2
    With Swift 2: `let hour = NSCalendar.currentCalendar().component(.Hour, fromDate: NSDate())` – Austin Moore Jan 18 '16 at 15:59
  • 2
    it's nice to see how Swift improves over time. Swift 3.0 version is way more readable that Swift 1.0 one – average Joe Jan 09 '17 at 09:50
  • You want to use "h a" not "hh a" If you want 7 pm not 07 pm – DoesData Feb 16 '18 at 00:46
  • How can I get all the hours in a day ? for example something like: let hoursArray = formatter.string(from: Date()) // 12 AM, 1 AM ... etc .. until all 24 hrs are there. – Hussein Apr 20 '22 at 12:42
  • The solution involving `DateFormatter` doesn't doesn't seem to be working in Swift 5.6. The line `formatter.dateFormat = "hh a"` throws a bunch of errors. – John Harrington Jun 14 '22 at 23:45
30

Swift 3:

let date = Date()// Aug 25, 2017, 11:55 AM
let calendar = Calendar.current

let hour = calendar.component(.hour, from: date) //11
let minute = calendar.component(.minute, from: date) //55
let sec = calendar.component(.second, from: date) //33
let weekDay = calendar.component(.weekday, from: date) //6 (Friday) 

Get any of component available from the API below

public enum Component {
    case era
    case year
    case month
    case day
    case hour
    case minute
    case second
    case weekday
    case weekdayOrdinal
    case quarter
    case weekOfMonth
    case weekOfYear
    case yearForWeekOfYear
    case nanosecond
    case calendar
    case timeZone
}

Swift 2:

let currentHour = NSCalendar.currentCalendar().component(.Hour, fromDate: NSDate())

This could be enough :

let currentDate = NSDate() // You can input the custom as well 
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate:  currentDate)
let currentHour = components.hour // You can play around with the ""components""
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
7

If you want the current hour in a String, this is as short and readable I could think of.

let formatter = NSDateFormatter()
formatter.dateFormat = "HH"

let timeString = formatter.stringFromDate(NSDate())
Binarian
  • 12,296
  • 8
  • 53
  • 84
  • I understand your code but after all, I would need an integer instead of a string, but thank you very much for your help, I believe that this code can be very useful – Samuel Gómez Jun 10 '14 at 10:04
1

Finally I was able to find the easiest solution after struggling for a time

let dateComponents = NSCalendar.currentCalendar().components(NSCalendarUnit.HourCalendarUnit, fromDate: NSDate())
let nHourOfDay = dateComponents.hour
Samuel Gómez
  • 877
  • 1
  • 6
  • 7
1

For Swift 2.0:

let hour = NSCalendar.currentCalendar().component(NSCalendarUnit.Hour, fromDate: NSDate())
Gmeister4
  • 754
  • 10
  • 19
1

Here is a reference example for how I do it (DateUtils.swift) -

Example Use:

    let today = DateUtils.getToday();
    let hr  = DateUtils.getHours(today);
    let min = DateUtils.getMinutes(today);
    ... (etc.) ...

DateUtils.swift:

//Field wrapper routines
class func getToday() -> Date               { return Date(); }
class func getHours(_ date : Date) -> Int   { return Calendar.current.component(.hour, from: date); }
class func getMinutes(_ date : Date) -> Int { return Calendar.current.component(.minute, from: date); }
... (continued for all fields, see file) ...
J-Dizzle
  • 4,861
  • 4
  • 40
  • 50
0

You can get the integer value of the current hour in one step like this:

let currentHour = NSCalendar.currentCalendar().components(.Hour, fromDate: NSDate()).hour
J. Cross
  • 1
  • 1