如何快速地获得当周的星期一的日期 [英] How to get Monday's date of the current week in swift

查看:183
本文介绍了如何快速地获得当周的星期一的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试星期一的当天的日期。这在我的表格视图中被视为一周的第一天。
我也需要得到本周的星期日。这被视为我的表格视图中的最后一天。

I'm trying to get Monday's date of the current week. This is treated as the first day of the week in my table view. I also need to get Sunday's of the current week. This is treated as the last day of the week in my table view.

当前尝试:

let date = NSDate()
let calendar = NSCalendar.currentCalendar()
calendar.firstWeekday = 1
//attempt to changefirstday

let dateFormatter = NSDateFormatter()
let theDateFormat = NSDateFormatterStyle.ShortStyle
let theTimeFormat = NSDateFormatterStyle.ShortStyle
dateFormatter.dateStyle = theDateFormat
dateFormatter.timeStyle = theTimeFormat

let currentDateComponents = calendar.components([.YearForWeekOfYear, .WeekOfYear ], fromDate: date)
let startOfWeek = calendar.dateFromComponents(currentDateComponents)
print("startOfWeek is \(startOfWeek)")
let stringDate = dateFormatter.stringFromDate(startOfWeek!)
print("string date is \(stringDate)") //This is returning Sunday's date


推荐答案

这是一个简单的功能,将giv你本周的星期一的日期,

Here is a simple function that will give you this week's Monday's date,

func getWeekDaysInEnglish() -> [String] {
    let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    calendar.locale = NSLocale(localeIdentifier: "en_US_POSIX")
    return calendar.weekdaySymbols
}

enum SearchDirection {
    case Next
    case Previous

    var calendarOptions: NSCalendarOptions {
        switch self {
            case .Next:
                return .MatchNextTime
            case .Previous:
                return [.SearchBackwards, .MatchNextTime]
        }
    }
}

func get(direction: SearchDirection, _ dayName: String, considerToday consider: Bool = false) -> NSDate {
    let weekdaysName = getWeekDaysInEnglish()

    assert(weekdaysName.contains(dayName), "weekday symbol should be in form \(weekdaysName)")

    let nextWeekDayIndex = weekdaysName.indexOf(dayName)! + 1 // weekday is in form 1 ... 7 where as index is 0 ... 6

    let today = NSDate()

    let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!

    if consider && calendar.component(.Weekday, fromDate: today) == nextWeekDayIndex {
        return today
    }

    let nextDateComponent = NSDateComponents()
    nextDateComponent.weekday = nextWeekDayIndex


    let date = calendar.nextDateAfterDate(today, matchingComponents: nextDateComponent, options: direction.calendarOptions)
    return date!
}

get(.Next, "Monday") // Nov 2, 2015, 12:00 AM
get(.Next, "Sunday") // Nov 1, 2015, 12:00 AM

get(.Previous, "Sunday") // Oct 25, 2015, 12:00 AM
get(.Previous, "Monday") // Oct 26, 2015, 12:00 AM

get(.Previous, "Thursday") // Oct 22, 2015, 12:00 AM
get(.Next, "Thursday") // Nov 5, 2015, 12:00 AM
get(.Previous, "Thursday", considerToday:  true) // // Oct 29, 2015, 12:00 AM

您可以使用此方法获取您提供的任何日期和日期名称,这些功能应该是这些,

You can use this method to get any days and day name you provide to the function should be either of these,

"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"

这篇关于如何快速地获得当周的星期一的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆