web-dev-qa-db-ja.com

Swiftで時刻を取得する方法は?

Swiftで時刻を取得するにはどうすればよいですか。 NSCalendarNSDateComponentsを試しましたが、Swiftで始めたばかりではないかと思います。

48
Samuel Gómez

Swift 5.0/4.0/3.0

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

または、12 hour AM/PM日付形式に関心がある場合は、 NSDateFormatter を使用します

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

分、秒などが必要な場合は、次のようにします

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)

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())
118
average Joe

スウィフト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) 

以下のAPIから利用可能なコンポーネントを取得します

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())

これで十分でしょう:

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""
26
Kumar KL

文字列で現在の時刻が必要な場合、これは短くて読みやすいと思います。

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

let timeString = formatter.stringFromDate(NSDate())
7
Binarian

ここに私がそれを行う方法の参考例があります( DateUtils.Swift )-

使用例:

    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) ...
1
J-Dizzle

最後に私はしばらく苦労した後、最も簡単な解決策を見つけることができました

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

Swift 2.0:

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

次のように、1時間で現在の時間の整数値を取得できます。

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