我创建了一个方法,该方法应采用“ YYYY-MM-DD”形式的字符串,并吐出一个int,该int表示相对于它所在的星期的日期位置(无论两个月之间是否重叠)。因此,例如,sunday = 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选项(或类似的东西)。