使用NSDate swift获取星期几

前端之家收集整理的这篇文章主要介绍了使用NSDate swift获取星期几前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好吧,所以我创建了一个方法,应该采取“YYYY-MM-DD”形式的字符串,并吐出一个int,表示相对于它所在的星期的日期位置(无论它是否在月份之间重叠)。所以例如星期日= 1星期一= 2等等。

这里是我的代码

func getDayOfWeek(today:String)->Int{

    var formatter:NSDateFormatter = NSDateFormatter()
    formatter.dateFormat = "YYYY-MM-DD"
    var todayDate:NSDate = formatter.dateFromString(today)!
    var myCalendar:NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
    var myComponents = myCalendar.components(NSCalendarUnit.WeekdayOrdinalCalendarUnit,fromDate: todayDate)
    var weekDay = myComponents.weekdayOrdinal
    return weekDay
}

所以我知道NSCalendarUnit.WeekdayOrdinalCalendar是错误的,但我试过我认为大多数逻辑组合。并且也搞乱了myComponents.weekdayOrdinal例如使用mycomponents.day或.weekday。

这里是我的选择在什么使用

static var EraCalendarUnit: NSCalendarUnit { get }
static var YearCalendarUnit: NSCalendarUnit { get }
static var MonthCalendarUnit: NSCalendarUnit { get }
static var DayCalendarUnit: NSCalendarUnit { get }
static var HourCalendarUnit: NSCalendarUnit { get }
static var MinuteCalendarUnit: NSCalendarUnit { get }
static var SecondCalendarUnit: NSCalendarUnit { get }
static var WeekCalendarUnit: NSCalendarUnit { get }
static var WeekdayCalendarUnit: NSCalendarUnit { get }
static var WeekdayOrdinalCalendarUnit: NSCalendarUnit { get }
static var QuarterCalendarUnit: NSCalendarUnit { get }
static var WeekOfMonthCalendarUnit: NSCalendarUnit { get }
static var WeekOfYearCalendarUnit: NSCalendarUnit { get }
static var YearForWeekOfYearCalendarUnit: NSCalendarUnit { get }
static var CalendarCalendarUnit: NSCalendarUnit { get }
static var TimeZoneCalendarUnit: NSCalendarUnit { get }

我不清楚,因为没有DayOfWeekUnit选项(或类似的东西)。
任何推动在正确的方向获得正确的数字将是非常感激。

你正在寻找(如果我正确理解的问题)是NSCalendarUnit.CalendarUnitWeekday。 NSDateComponents的相应属性是工作日。

请注意,您的日期格式错误(
完整规格可以在这里找到:http://unicode.org/reports/tr35/tr35-6.html)。

使用自动类型推理可以简化函数,也可以在常量足够的地方使用变量:

func getDayOfWeek(today:String)->Int {

    let formatter  = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    let todayDate = formatter.dateFromString(today)!
    let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let myComponents = myCalendar.components(.Weekday,fromDate: todayDate)
    let weekDay = myComponents.weekday
    return weekDay
}

let weekday = getDayOfWeek("2014-08-27")
print(weekday) // 4 = Wednesday

然而,如果输入字符串无效,那么该函数在运行时会崩溃。正如Mike S正确地说,返回一个可选的是Swift的方式来处理这种情况:

func getDayOfWeek(today:String)->Int? {

    let formatter  = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    if let todayDate = formatter.dateFromString(today) {
        let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
        let myComponents = myCalendar.components(.Weekday,fromDate: todayDate)
        let weekDay = myComponents.weekday
        return weekDay
    } else {
        return nil
    }
}

其中将使用如:

if let weekday = getDayOfWeek("2014-08-27") {
    print(weekday)
} else {
    print("bad input")
}

(为Swift 2 / Xcode 7更新)

更新Swift 3(Xcode 8):

func getDayOfWeek(_ today:String) -> Int? {
    let formatter  = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    guard let todayDate = formatter.date(from: today) else { return nil }
    let myCalendar = Calendar(identifier: .gregorian)
    let weekDay = myCalendar.component(.weekday,from: todayDate)
    return weekDay
}
原文链接:https://www.f2er.com/swift/320877.html

猜你在找的Swift相关文章