如何使用Swift获取当天的时间?

时间:2014-06-10 09:39:20

标签: ios swift nsdate

我如何在Swift中获得一天的时间。 我尝试过NSCalendarNSDateComponents,但我担心我只是从Swift开始。

7 个答案:

答案 0 :(得分:111)

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

答案 1 :(得分:26)

斯威夫特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""

答案 2 :(得分:7)

如果你想要一个字符串中的当前小时,这是我能想到的那么简短和可读。

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

let timeString = formatter.stringFromDate(NSDate())

答案 3 :(得分:1)

最后,经过一段时间的努力,我找到了最简单的解决方案

let dateComponents = NSCalendar.currentCalendar().components(NSCalendarUnit.HourCalendarUnit, fromDate: NSDate())
let nHourOfDay = dateComponents.hour

答案 4 :(得分:1)

对于Swift 2.0:

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

答案 5 :(得分:1)

以下是我如何做的参考示例(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) ...

答案 6 :(得分:0)

您可以像这样一步获取当前小时的整数值:

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