Swift - Start day and end day of the week before previous week - swift

Today is Friday 6 March. How to find that 16 Feb is the start day and 22 Feb is the end day of the week before previous week.
16 is for my country Bulgaria in USA will be 15 and 21 I use .currentCalendar()

Something like this should work:
let cal = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.weekOfYear -= 1
if let date = cal.dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions(0)) {
var beginningOfWeek: NSDate?
var weekDuration = NSTimeInterval()
if cal.rangeOfUnit(.CalendarUnitWeekOfYear, startDate: &beginningOfWeek, interval: &weekDuration, forDate: date) {
let endOfWeek = beginningOfWeek?.dateByAddingTimeInterval(weekDuration)
print(beginningOfWeek) // Optional(2015-02-15 05:00:00 +0000)
print(endOfWeek) // Optional(2015-02-22 05:00:00 +0000)
}
}

A simpler Swift 2 alternative as an extension, derived from Martin R's answer for getting the end of the month:
extension: NSDate {
func startOfWeek(weekday: Int?) -> NSDate? {
guard
let cal: NSCalendar = NSCalendar.currentCalendar(),
let comp: NSDateComponents = cal.components([.YearForWeekOfYear, .WeekOfYear], fromDate: self) else { return nil }
comp.to12pm()
cal.firstWeekday = weekday ?? 1
return cal.dateFromComponents(comp)!
}
func endOfWeek(weekday: Int) -> NSDate? {
guard
let cal: NSCalendar = NSCalendar.currentCalendar(),
let comp: NSDateComponents = cal.components([.WeekOfYear], fromDate: self) else { return nil }
comp.weekOfYear = 1
comp.day -= 1
comp.to12pm()
return cal.dateByAddingComponents(comp, toDate: self.startOfWeek(weekday)!, options: [])!
}
}
Usage:
// Use 2 for Monday, 1 for Sunday:
print(NSDate().startOfWeek(1)!) // "2016-01-24 08:00:00 +0000\n"
print(NSDate().endOfWeek(1)!) // "2016-01-30 08:00:00 +0000\n"
To guard against DST and such, you should also implement an extension to force the time to 12 pm:
internal extension NSDateComponents {
func to12pm() {
self.hour = 12
self.minute = 0
self.second = 0
}
}

If anyone looking for swift 3 answer:
func startOfWeek(weekday: Int?) -> Date {
var cal = Calendar.current
var component = cal.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)
component.to12am()
cal.firstWeekday = weekday ?? 1
return cal.date(from: component)!
}
func endOfWeek(weekday: Int) -> Date {
let cal = Calendar.current
var component = DateComponents()
component.weekOfYear = 1
component.day = -1
component.to12pm()
return cal.date(byAdding: component, to: startOfWeek(weekday: weekday))!
}
I set to 00:00:00 of that day
internal extension DateComponents {
mutating func to12am() {
self.hour = 0
self.minute = 0
self.second = 0
}
mutating func to12pm(){
self.hour = 23
self.minute = 59
self.second = 59
}
}

Swift 4+:
A very straightforward approach using mainly Calendar with the help of DateComponents:
Get Date
Go to previous week
Calendar.current.date(byAdding: .weekdayOrdinal, value: -1, to: aDate)
Get the current weekday
Calendar.current.component(.weekday, from: bDate)
Compute an offset from this weekday
Negative offset to reach the start of week
Positive offset to the reach end of the week
Use offset to go to the required date
Calendar.current.date(byAdding: .day, value: offset, to: bDate)
Solution:
extension Date {
var firstWeekdayOfLastWeek: Date? {
guard let previousWeek = Calendar.current.date(byAdding: .weekdayOrdinal,
value: -1, to: self)
else { return nil }
let offsetToFirstWeekday: Int = {
let currentWeekday = Calendar.current.component(.weekday, from: previousWeek)
guard currentWeekday > 0 else { return 0 }
return 1 - currentWeekday
}()
let result = Calendar.current.date(byAdding: .day,
value: offsetToFirstWeekday,
to: previousWeek)
return result
}
var lastWeekdayOfLastWeek: Date? {
guard let previousWeek = Calendar.current.date(byAdding: .weekdayOrdinal,
value: -1, to: self)
else { return nil }
let offsetToLastWeekday: Int = {
let currentWeekday = Calendar.current.component(.weekday, from: previousWeek)
return 7 - currentWeekday
}()
let result = Calendar.current.date(byAdding: .day,
value: offsetToLastWeekday,
to: previousWeek)
return result
}
}
Usage Example:
let formatter = DateFormatter()
formatter.dateStyle = .long
let date = Date()
//My current system date (as of time of writing)
print(formatter.string(from: currentDate)) //December 11, 2020
//Results (my system has Sunday as the start of the week)
print(formatter.string(from: date.firstWeekdayOfLastWeek!)) //November 29, 2020
print(formatter.string(from: date.lastWeekdayOfLastWeek!)) //December 5, 2020
Force unwrap is for example purposes only

Related

How to round up date to midnight in Swift? [duplicate]

I'm trying to get the first and last day of the month in swift.
So far I have the following:
let dateFormatter = NSDateFormatter()
let date = NSDate()
dateFormatter.dateFormat = "yyyy-MM-dd"
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: date)
let month = components.month
let year = components.year
let startOfMonth = ("\(year)-\(month)-01")
But I'm not sure how to get the last date. Is there a built in method I'm missing? Obviously it has to take into account leap years etc.
Swift 3 and 4 drop-in extensions
This actually gets a lot easier with Swift 3+:
You can do it without guard (you could if you wanted to, but because DateComponents is a non-optional type now, it's no longer necessary).
Using iOS 8's startOfDayForDate (now startOfDay), you don't need to manually set the time to 12pm unless you're doing some really crazy calendar calculations across time zones.
It's worth mentioning that some of the other answers claim you can shortcut this by using Calendar.current.date(byAdding: .month, value: 0, to: Date())!, but where this fails, is that it doesn't actually zero out the day, or account for differences in timezones.
Here you go:
extension Date {
func startOfMonth() -> Date {
return Calendar.current.date(from: Calendar.current.dateComponents([.year, .month], from: Calendar.current.startOfDay(for: self)))!
}
func endOfMonth() -> Date {
return Calendar.current.date(byAdding: DateComponents(month: 1, day: -1), to: self.startOfMonth())!
}
}
print(Date().startOfMonth()) // "2018-02-01 08:00:00 +0000\n"
print(Date().endOfMonth()) // "2018-02-28 08:00:00 +0000\n"
You get the first day of the month simply with
let components = calendar.components([.Year, .Month], fromDate: date)
let startOfMonth = calendar.dateFromComponents(components)!
print(dateFormatter.stringFromDate(startOfMonth)) // 2015-11-01
To get the last day of the month, add one month and subtract one day:
let comps2 = NSDateComponents()
comps2.month = 1
comps2.day = -1
let endOfMonth = calendar.dateByAddingComponents(comps2, toDate: startOfMonth, options: [])!
print(dateFormatter.stringFromDate(endOfMonth)) // 2015-11-30
Alternatively, use the rangeOfUnit method which gives you
the start and the length of the month:
var startOfMonth : NSDate?
var lengthOfMonth : NSTimeInterval = 0
calendar.rangeOfUnit(.Month, startDate: &startOfMonth, interval: &lengthOfMonth, forDate: date)
For a date on the last day of month, add the length of the month minus one second:
let endOfMonth = startOfMonth!.dateByAddingTimeInterval(lengthOfMonth - 1)
Updated for Swift5:
extension Date {
var startOfDay: Date {
return Calendar.current.startOfDay(for: self)
}
var startOfMonth: Date {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents([.year, .month], from: self)
return calendar.date(from: components)!
}
var endOfDay: Date {
var components = DateComponents()
components.day = 1
components.second = -1
return Calendar.current.date(byAdding: components, to: startOfDay)!
}
var endOfMonth: Date {
var components = DateComponents()
components.month = 1
components.second = -1
return Calendar(identifier: .gregorian).date(byAdding: components, to: startOfMonth)!
}
func isMonday() -> Bool {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents([.weekday], from: self)
return components.weekday == 2
}
}
With Swift 3 & iOS 10 the easiest way I found to do this is Calendar's dateInterval(of:for:):
guard let interval = calendar.dateInterval(of: .month, for: Date()) else { return }
You can then use interval.start and interval.end to get the dates you need.
Swift 3
Many date example for :
Last 6 month,
last 3 month,
yesterday, last 7 day, last 30 day, previous month,
current month start & end, last month start & end date
let startDate = dateFormatter.string(from: Date().getThisMonthStart()!)
let endDate = dateFormatter.string(from: Date().getThisMonthEnd()!)
extension Date {
func getLast6Month() -> Date? {
return Calendar.current.date(byAdding: .month, value: -6, to: self)
}
func getLast3Month() -> Date? {
return Calendar.current.date(byAdding: .month, value: -3, to: self)
}
func getYesterday() -> Date? {
return Calendar.current.date(byAdding: .day, value: -1, to: self)
}
func getLast7Day() -> Date? {
return Calendar.current.date(byAdding: .day, value: -7, to: self)
}
func getLast30Day() -> Date? {
return Calendar.current.date(byAdding: .day, value: -30, to: self)
}
func getPreviousMonth() -> Date? {
return Calendar.current.date(byAdding: .month, value: -1, to: self)
}
// This Month Start
func getThisMonthStart() -> Date? {
let components = Calendar.current.dateComponents([.year, .month], from: self)
return Calendar.current.date(from: components)!
}
func getThisMonthEnd() -> Date? {
let components:NSDateComponents = Calendar.current.dateComponents([.year, .month], from: self) as NSDateComponents
components.month += 1
components.day = 1
components.day -= 1
return Calendar.current.date(from: components as DateComponents)!
}
//Last Month Start
func getLastMonthStart() -> Date? {
let components:NSDateComponents = Calendar.current.dateComponents([.year, .month], from: self) as NSDateComponents
components.month -= 1
return Calendar.current.date(from: components as DateComponents)!
}
//Last Month End
func getLastMonthEnd() -> Date? {
let components:NSDateComponents = Calendar.current.dateComponents([.year, .month], from: self) as NSDateComponents
components.day = 1
components.day -= 1
return Calendar.current.date(from: components as DateComponents)!
}
}
Swift 4
If you only need the ordinal day:
func lastDay(ofMonth m: Int, year y: Int) -> Int {
let cal = Calendar.current
var comps = DateComponents(calendar: cal, year: y, month: m)
comps.setValue(m + 1, for: .month)
comps.setValue(0, for: .day)
let date = cal.date(from: comps)!
return cal.component(.day, from: date)
}
lastDay(ofMonth: 2, year: 2018) // 28
lastDay(ofMonth: 2, year: 2020) // 29
This is the simplest way that I found (Swift 5+):
extension Date {
func getStart(of component: Calendar.Component, calendar: Calendar = Calendar.current) -> Date? {
return calendar.dateInterval(of: component, for: self)?.start
}
func getEnd(of component: Calendar.Component, calendar: Calendar = Calendar.current) -> Date? {
return calendar.dateInterval(of: component, for: self)?.end
}
}
Here is easiest solution:
extension Date {
func startOfMonth() -> Date {
let interval = Calendar.current.dateInterval(of: .month, for: self)
return (interval?.start.toLocalTime())! // Without toLocalTime it give last months last date
}
func endOfMonth() -> Date {
let interval = Calendar.current.dateInterval(of: .month, for: self)
return interval!.end
}
// Convert UTC (or GMT) to local time
func toLocalTime() -> Date {
let timezone = TimeZone.current
let seconds = TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}}
and then call these with your date instance:
print(Date().startOfMonth())
print(Date().endOfMonth())
2017...
First, get the month you need:
let cal = Calendar.current
let d = Calendar.current.date(byAdding: .month, value: 0, to: Date())!
// for "last month" just use -1, for "next month" just use 1, etc
To get the day-of-the-week for the first day of the month:
let c = cal.dateComponents([.year, .month], from: d)
let FDOM = cal.date(from: c)!
let dowFDOM = cal.component(.weekday, from: FDOM)
print("the day-of-week on the 1st is ... \(dowFDOM)")
// so, that's 1=Sunday, 2=Monday, etc.
To get the number of days in the month:
let r = cal.range(of: .day, in: .month, for: d)!
let kDays = r.count
print("the number of days is ... \(kDays)")
With Swift 3, you can choose one of the two following patters in order to retrieve the first and last days of a month.
#1. Using Calendar dateComponents(_:from:), date(from:) and date(byAdding:to:wrappingComponents:) methods
With this pattern, you first get the date of the first day of a month then add a month and remove a day from it in order to get the date of the last day of the month. The Playground code below shows how to set it:
import Foundation
// Set calendar and date
let calendar = Calendar.current
let date = calendar.date(byAdding: DateComponents(day: -10), to: Date())!
// Get first day of month
let firstDayComponents = calendar.dateComponents([.year, .month], from: date)
let firstDay = calendar.date(from: firstDayComponents)!
// Get last day of month
let lastDayComponents = DateComponents(month: 1, day: -1)
let lastDay = calendar.date(byAdding: lastDayComponents, to: firstDay)!
// Set date formatter
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_UK")
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .long
// Print results
print(dateFormatter.string(from: date)) // Prints: 22 March 2017 at 18:07:15 CET
print(dateFormatter.string(from: firstDay)) // Prints: 1 March 2017 at 00:00:00 CET
print(dateFormatter.string(from: lastDay)) // Prints: 31 March 2017 at 00:00:00 CEST
#2. Using Calendar range(of:in:for:), dateComponents(_:from:) and date(from:) and methods
With this pattern, you get a range of absolute day values in a month and then retrieve the dates of the first day and last day of the month from it. The Playground code below shows how to set it:
import Foundation
// Set calendar and date
let calendar = Calendar.current
let date = calendar.date(byAdding: DateComponents(day: -10), to: Date())!
// Get range of days in month
let range = calendar.range(of: .day, in: .month, for: date)! // Range(1..<32)
// Get first day of month
var firstDayComponents = calendar.dateComponents([.year, .month], from: date)
firstDayComponents.day = range.lowerBound
let firstDay = calendar.date(from: firstDayComponents)!
// Get last day of month
var lastDayComponents = calendar.dateComponents([.year, .month], from: date)
lastDayComponents.day = range.upperBound - 1
//lastDayComponents.day = range.count // also works
let lastDay = calendar.date(from: lastDayComponents)!
// Set date formatter
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_UK")
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .long
// Print results
print(dateFormatter.string(from: date)) // prints: 22 March 2017 at 18:07:15 CET
print(dateFormatter.string(from: firstDay)) // prints: 1 March 2017 at 00:00:00 CET
print(dateFormatter.string(from: lastDay)) // prints: 31 March 2017 at 00:00:00 CEST
In swift 3, if you put 0 to day component you can get the last day of the month. There's an example code:
public func isMoreDays(date: Date, asc: Bool)->Bool{
//components
var dayComponents = self.getDateComponents(date: date)
//asc is true if ascendant or false if descendant
dayComponents.day = asc ? 0 : 1
//plus 1 to month 'cos if you set up day to 0 you are going to the previous month
dayComponents.month = asc ? dayComponents.month! + 1 : dayComponents.month
//instantiate calendar and get the date
let calendar : Calendar = NSCalendar.current
let day = calendar.date(from: dayComponents)
//date comparison
if(day?.compare(date) == .orderedSame){
return false
}
return true
}
You can use the following extensions here :
let today = Date()
let startOfMonth = today.beginning(of: .month)
let endOfMonth = today.end(of: .month)

How to get all days in current week in swift

I am making an application where I need to get the day of the year for all days in the current week.
To achieve this, I am looking for a result similar to below (Today is Thursday the 23rd of March)
Monday = 79
Tuesday = 80
Wednesday = 81
Thursday = 82
Friday = 83
Saturday and Sunday can be included, however my application only needs weekdays rather then weekends. Today is day 82
To get the weekdays of the week, it is:
let calendar = Calendar.current
let today = calendar.startOfDay(for: Date())
let dayOfWeek = calendar.component(.weekday, from: today)
let dates = calendar.range(of: .weekday, in: .weekOfYear, for: today)!
.compactMap { calendar.date(byAdding: .day, value: $0 - dayOfWeek, to: today) }
.filter { !calendar.isDateInWeekend($0) }
To display that as “Thursday = 82”, it is:
let formatter = DateFormatter()
formatter.dateFormat = "eeee' = 'D"
for date in dates {
print(formatter.string(from: date))
}
Or
let strings = dates.map { formatter.string(from: $0) }
If you want it to consider firstWeekday (generally only a concern if you are no longer filtering weekends out of the results, hence no filter on isDateInWeekend, below):
let calendar = Calendar.current
let today = calendar.startOfDay(for: Date())
let todaysDayOfWeek = calendar.component(.weekday, from: today)
guard
let weekdaysRange = calendar.range(of: .weekday, in: .weekOfYear, for: today),
let index = weekdaysRange.firstIndex(of: calendar.firstWeekday)
else { return }
let weekdays = weekdaysRange[index...] + weekdaysRange[..<index].map { $0 + weekdaysRange.count }
let dates = weekdays.compactMap { calendar.date(byAdding: .day, value: $0 - todaysDayOfWeek, to: today) }
I had problems with adapting Rob's solution for when the week starts on Monday. This solution adapts to your calendar.firstWeekday setting.
var calendar = Calendar.autoupdatingCurrent
calendar.firstWeekday = 2 // Start on Monday (or 1 for Sunday)
let today = calendar.startOfDay(for: Date())
var week = [Date]()
if let weekInterval = calendar.dateInterval(of: .weekOfYear, for: today) {
for i in 0...6 {
if let day = calendar.date(byAdding: .day, value: i, to: weekInterval.start) {
week += [day]
}
}
}
return week
I made a small extension on Calendar, which might be helpful for anyone else looking for a more functional solution to this.
import Foundation
extension Calendar {
func intervalOfWeek(for date: Date) -> DateInterval? {
dateInterval(of: .weekOfYear, for: date)
}
func startOfWeek(for date: Date) -> Date? {
intervalOfWeek(for: date)?.start
}
func daysWithSameWeekOfYear(as date: Date) -> [Date] {
guard let startOfWeek = startOfWeek(for: date) else {
return []
}
return (0 ... 6).reduce(into: []) { result, daysToAdd in
result.append(Calendar.current.date(byAdding: .day, value: daysToAdd, to: startOfWeek))
}
.compactMap { $0 }
}
}
You could also use enumerateDates like this:
func getAllDaysOfTheCurrentWeek() -> [Date] {
var dates: [Date] = []
guard let dateInterval = Calendar.current.dateInterval(of: .weekOfYear,
for: Date()) else {
return dates
}
Calendar.current.enumerateDates(startingAfter: dateInterval.start,
matching: DateComponents(hour:0),
matchingPolicy: .nextTime) { date, _, stop in
guard let date = date else {
return
}
if date <= dateInterval.end {
dates.append(date)
} else {
stop = true
}
}
return dates
}

A method which returns an array of dates that are in between two dates - Swift 3

I'm trying to get an array of dates between the time stamp of one of my objects and 30 days into the future.
I've used the code below but i'm not getting the desired result and am having trouble trying to make a method described in the title. Any help would be great, thank you.
var dates = [Date]()
func fetchDays() {
let cal = Calendar.current
var dateComponents = DateComponents()
dateComponents.year = 2017
dateComponents.month = 2
dateComponents.day = 12
guard let startDate = cal.date(from: dateComponents) else {
return }
var start = cal.startOfDay(for: startDate)
for _ in 0 ... 30 {
guard let daysBetween = cal.date(byAdding: .day, value: 1, to: startDate) else { return }
start = daysBetween
dates.append(start)
}
}
You are adding 1 to the same start date so your array is filled with the same date over and over. Simply replace 1 with the loop index + 1.
for i in 0 ... 30 {
if let newDate = cal.date(byAdding: .day, value: i + 1, to: startDate) {
dates.append(newDate)
}
}
And you don't need the start variable.
Hi #Breezy to make it work you need only to change a little thing
change the value of to parameter for start like this:
for _ in 0 ... 30 {
guard let daysBetween = cal.date(byAdding: .day, value: 1, to: start) else { return }
start = daysBetween
dates.append(start)
}
Edited:
if you don't want to use 30 days you can add a month end then get the days between the 2 dates like this:
var dates = [Date]()
func fetchDays() {
let cal = Calendar.current
var dateComponents = DateComponents()
dateComponents.year = 2017
dateComponents.month = 2
dateComponents.day = 12
guard let startDate = cal.date(from: dateComponents) else {
return }
var start = cal.startOfDay(for: startDate)
guard let endDate = cal.date(byAdding: .month, value: 1, to: start) else { return }
guard let daysBetween = cal.dateComponents([.day], from: start, to: endDate).day else { return }
for _ in 0 ... daysBetween {
guard let newDate = cal.date(byAdding: .day, value: 1, to: start) else { return }
start = newDate
dates.append(newDate)
}
}

End Date showing Wrong

I want to find start date and end date of week I am geting start date but not find right end date of week
startDate = NSDate().startOfWeek(2)
print(startDate)
endDate = startDate.endOfWeek(3)
print(endDate)
I am getting this start date -2016-07-04 06:30:00 +0000 and end date - 5828963-12-20 00:00:00 +0000
extension NSDate {
func startOfWeek(weekday: Int?) -> NSDate? {
guard
let cal: NSCalendar = NSCalendar.currentCalendar(),
let comp: NSDateComponents = cal.components([.YearForWeekOfYear, .WeekOfYear], fromDate: self) else { return nil }
comp.to12pm()
cal.firstWeekday = weekday ?? 1
return cal.dateFromComponents(comp)!
}
func endOfWeek(weekday: Int) -> NSDate? {
guard
let cal: NSCalendar = NSCalendar.currentCalendar(),
let comp: NSDateComponents = cal.components([.WeekOfYear], fromDate: self) else { return nil }
comp.weekOfYear = 1
comp.day -= 1
comp.to12pm()
return cal.dateByAddingComponents(comp, toDate: self.startOfWeek(weekday)!, options: [])!
}
}
internal extension NSDateComponents {
func to12pm() {
self.hour = 12
self.minute = 0
self.second = 0
}
}
extension Date {
enum Weekday: Int {
case sunday = 1, monday, tuesday, wednesday, thursday, friday, saturday
}
func startOfWeek(_ firstWeekday: Weekday = .sunday) -> Date? {
var cal = Calendar.current
var dateComponents = cal.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)
dateComponents.setTimeComponentsToNoon()
cal.firstWeekday = firstWeekday.rawValue
return cal.date(from: dateComponents)
}
func endOfWeek(_ firstWeekday: Weekday = .sunday) -> Date? {
guard let startOfWeek = startOfWeek(firstWeekday) else { return nil }
var dateComponents = DateComponents()
dateComponents.weekOfYear = 1
dateComponents.day = -1
return Calendar.current.date(byAdding: dateComponents, to: startOfWeek)
}
}
extension DateComponents {
mutating func setTimeComponentsToNoon() {
self.hour = 12
self.minute = 0
self.second = 0
self.nanosecond = 0
}
}
Testing:
if let startDate = Date().startOfWeek(.monday),
let endDate = Date().endOfWeek(.monday) {
print(startDate) // "2020-02-03 15:00:00 +0000\n"
print(endDate) // "2020-02-09 15:00:00 +0000\n"
}
if let startDate = Date().startOfWeek(),
let endDate = Date().endOfWeek() {
print(startDate) // "2020-02-02 15:00:00 +0000\n"
print(endDate) // "2020-02-08 15:00:00 +0000\n"
}

Calculate number of weeks in a given month - Swift

Having looked at a few different suggestions on SO, I've not been able to determine why the function below does not work. It seems to return 6 for some months and 5 for others. When changing weeks for days it works perfectly.
For example, trying
weeksInMonth(8, forYear 2015)
Results in 6.
I believe I have a mis-understanding of what 'firstWeekday' property on calendar does but haven't found an adequate explanation by Apple or online.
Have tried both .WeekOfMonth and .WeekOfYear. Again can't find explanation of exact difference.
Any suggestions would be greatly welcome.
func weeksInMonth(month: Int, forYear year: Int) -> Int?
{
if (month < 1 || month > 12) { return nil }
let dateString = String(format: "%4d/%d/01", year, month)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd"
if let date = dateFormatter.dateFromString(dateString),
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
{
calendar.firstWeekday = 2 // Monday
let weekRange = calendar.rangeOfUnit(.WeekOfMonth, inUnit: .Month, forDate: date)
let weeksCount = weekRange.length
return weeksCount
}
else
{
return nil
}
}
Update:
Apologies my question was not clear. I'm trying to work out how many weeks there are in a month that include a Monday in them. For August this should be 5.
Your code computes the number of weeks which occur (complete or partially)
in a month. What you apparently want is the number of Mondays
in the given month. With NSDateComponents and in particular with
the weekdayOrdinal property you can compute the first
(weekdayOrdinal=1) and last (weekdayOrdinal=-1) Monday
in a month. Then compute the difference in weeks (and add one).
A possible implementation in Swift 2:
func numberOfMondaysInMonth(month: Int, forYear year: Int) -> Int?
{
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
calendar.firstWeekday = 2 // 2 == Monday
// First monday in month:
let comps = NSDateComponents()
comps.month = month
comps.year = year
comps.weekday = calendar.firstWeekday
comps.weekdayOrdinal = 1
guard let first = calendar.dateFromComponents(comps) else {
return nil
}
// Last monday in month:
comps.weekdayOrdinal = -1
guard let last = calendar.dateFromComponents(comps) else {
return nil
}
// Difference in weeks:
let weeks = calendar.components(.WeekOfMonth, fromDate: first, toDate: last, options: [])
return weeks.weekOfMonth + 1
}
Note: That a negative weekdayOrdinal counts backwards from the end of the month is not apparent form the documentation. It was observed in
Determine NSDate for Memorial Day in a given year and confirmed by Dave DeLong).
Update for Swift 3:
func numberOfMondaysInMonth(_ month: Int, forYear year: Int) -> Int? {
var calendar = Calendar(identifier: .gregorian)
calendar.firstWeekday = 2 // 2 == Monday
// First monday in month:
var comps = DateComponents(year: year, month: month,
weekday: calendar.firstWeekday, weekdayOrdinal: 1)
guard let first = calendar.date(from: comps) else {
return nil
}
// Last monday in month:
comps.weekdayOrdinal = -1
guard let last = calendar.date(from: comps) else {
return nil
}
// Difference in weeks:
let weeks = calendar.dateComponents([.weekOfMonth], from: first, to: last)
return weeks.weekOfMonth! + 1
}
Actually your question is : How many Mondays are in a given month?
My approach is to calculate the first Monday for the month, this can be accomplished by setting the CalendarUnit WeekdayOrdinal to 1. Then get the number of total days and do some math.
Swift 1.2
func mondaysInMonth(month: Int, forYear year: Int) -> Int?
{
if 1...12 ~= month {
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let components = NSDateComponents()
components.weekday = 2 // Monday
components.weekdayOrdinal = 1
components.month = month
components.year = year
if let date = calendar.dateFromComponents(components) {
let firstDay = calendar.component(.CalendarUnitDay, fromDate: date)
let days = calendar.rangeOfUnit(.CalendarUnitDay, inUnit:.CalendarUnitMonth, forDate:date).length
return (days - firstDay) / 7 + 1
}
}
return nil
}
Swift 2
func mondaysInMonth(month: Int, forYear year: Int) -> Int?
{
guard 1...12 ~= month else { return nil }
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let components = NSDateComponents()
components.weekday = 2 // Monday
components.weekdayOrdinal = 1
components.month = month
components.year = year
if let date = calendar.dateFromComponents(components) {
let firstDay = calendar.component(.Day, fromDate: date)
let days = calendar.rangeOfUnit(.Day, inUnit:.Month, forDate:date).length
return (days - firstDay) / 7 + 1
}
return nil
}
Swift 2
The following code calculates the number of weeks in a month. It does not depend on what day the month started or ended.
func weeksInMonth(month: Int, year: Int) -> (Int)? {
let calendar = NSCalendar.currentCalendar()
let comps = NSDateComponents()
comps.month = month+1
comps.year = year
comps.day = 0
guard let last = calendar.dateFromComponents(comps) else {
return nil
}
// Note: Could get other options as well
let tag = calendar.components([.WeekOfMonth,.WeekOfYear,
.YearForWeekOfYear,.Weekday,.Quarter],fromDate: last)
return tag.weekOfMonth
}
// Example Usage:
if let numWeeks = weeksInMonth(8,year: 2015) {
print(numWeeks) // Prints 6
}
if let numWeeks = weeksInMonth(12,year: 2015) {
print(numWeeks) // Prints 5
}
Swift 4+:
//-- Get number of weeks from calendar
func numberOfWeeksInMonth(_ date: Date) -> Int {
var calendar = Calendar(identifier: .gregorian)
calendar.firstWeekday = 1
let weekRange = calendar.range(of: .weekOfMonth,
in: .month,
for: date)
return weekRange!.count
}
//-- Implementation
let weekCount = numberOfWeeksInMonth(Date)
Swift 4+:
Generate Date from your components:
let dateComponents = DateComponents.init(year: 2019, month: 5)
let monthCurrentDayFirst = Calendar.current.date(from: dateComponents)!
let monthNextDayFirst = Calendar.current.date(byAdding: .month, value: 1, to: monthCurrentDayFirst)!
let monthCurrentDayLast = Calendar.current.date(byAdding: .day, value: -1, to: monthNextDayFirst)!
Detect week number from date:
let numberOfWeeks = Calendar.current.component(.weekOfMonth, from: monthCurrentDayLast)