【问题标题】:NSDateComponents in Swift is not returning WeekdaySwift 中的 NSDateComponents 不返回工作日
【发布时间】:2014-11-14 07:22:55
【问题描述】:
import UIKit

let components = NSDateComponents()
components.setValue(1, forComponent: NSCalendarUnit.CalendarUnitDay)
components.setValue(1, forComponent: NSCalendarUnit.CalendarUnitMonth)
components.setValue(2014, forComponent: NSCalendarUnit.CalendarUnitYear)

println(components.weekday) // 9223372036854775807

任何人对我为什么得到9223372036854775807而不是1-7的值有任何想法吗?

【问题讨论】:

  • 您需要知道某个日期的星期几吗?
  • 你从来没有提取过工作日。
  • 来自spec:NSDateComponents 的实例不负责回答有关超出其初始化信息的日期的问题。例如,如果你用 2004 年 5 月 6 日初始化一个,它的工作日是 NSUndefinedDateComponent,而不是星期四。
  • 他可以轻松提取工作日,正如您在我的回答中看到的那样
  • 那我怎样才能得到任意日期的工作日呢?

标签: ios swift nsdate nsdatecomponents


【解决方案1】:
let dateStyler = NSDateFormatter()
dateStyler.dateFormat = "yyyy-MM-dd"

let myDate = dateStyler.dateFromString("2014-01-01")!

let myWeekday = NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitWeekday, fromDate: myDate).weekday

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2013-05-07
    • 1970-01-01
    • 2014-12-16
    • 2011-12-27
    • 1970-01-01
    • 2015-06-06
    相关资源
    最近更新 更多