How to get the beginning and end of this week in SWIFT?

See the answer in English> How do I find the beginning of the week from an NSDate? 4 four
> How to get the current Monday’s date of the week in wift How to get the start and end of the week in swift?
This is my code

extension Date {
var startOfWeek: Date? {
let gregorian = Calendar(identifier:. gregorian)
guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else {return nil }
return gregorian.date(byAdding:. day, value: 1, to: sunday)
}

var endOfWeek: Date? {
let gregorian = Calendar(identifier: .gregorian)
guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else {return nil }
return gregorian.date(byAdding: .day, value: 7, to: sunday )
}
}

I call like this

let startWeek = Date().startOfWeek
let endWeek = Date().endOfWeek
print(startWeek!)
print(endWeek!)
var dateFormat1 = DateFormatter()
dateFormat1.dateFormat = "dd-MM-yy"< br />let startWeek2 = dateFormat1.string(from: startWe ek!)
var dateFormat12 = DateFormatter()
dateFormat12.dateFormat = "dd-MM-yy"
let endWeek2 = dateFormat12.string(from: endWeek!)
print( startWeek2)
print(endWeek2)

and output

2017-09-24 18:30:00 +0000
2017 -09-30 18:30:00 +0000
25-09-17
01-10-17

Your code works very well. If you want to run the code in the Playground, you can see the actual Date object instead of the String representation. Even if you don’t specify a DateFormatter, execute print(Date( )), Swift uses DateFormatter to print the Date object, so the value will be a relative time instead of an absolute point in time provided by the date object.

In the playground, you can see The actual startOfWeek is “September 25, 2017 at 12:00 AM”, and endOfWeek is “October 1, 2017 at 12:00 AM”, which is the correct value for this week.

Do not use print(Date()) to check the correctness of the Date object, because as you can see, due to the use of the basic DateFormatters, it will produce unexpected results.

See English answer> How do I find the beginning of the week from an NSDate? How do I get the end of this week from an NSDate? How to get the beginning of the week and how to get Monday’s date of the current week in swift
This is my code

extension Date {
var startOfWeek: Date? {
let gregorian = Calendar(identifier:. gregorian)
guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else {return nil }
return gregorian.date(byAdding:. day, value: 1, to: sunday)
}

var endOfWeek: Date? {
let gregorian = Calendar(identifier: .gregorian)
guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else {return nil }
return gregorian.date(byAdding: .day, value: 7, to: sunday )
}
}

I call like this

let startWeek = Date().startOfWeek
let endWeek = Date().endOfWeek
print(startWeek!)
print(endWeek!)
var dateFormat1 = DateFormatter()
dateFormat1.dateFormat = "dd-MM-yy"< br />let startWeek2 = dateFormat1.string(from: startWeek!)var dateFormat12 = DateFormatter()
dateFormat12.dateFormat = "dd-MM-yy"
let endWeek2 = dateFormat12.string(from: endWeek!)
print(startWeek2)
print(endWeek2)

and output

2017-09-24 18:30:00 +0000
2017-09-30 18:30:00 +0000
25-09-17
01-10-17

Your code works very well If you want to run the code in the Playground, you can see the actual Date object instead of the String representation. Even if you don’t specify a DateFormatter, when you execute print(Date()), Swift uses the DateFormatter to print the Date object, so the value will be It is a relative time rather than an absolute time point provided by the date object.

In the playground, you can see that the actual startOfWeek is “September 25, 2017 at 12:00 AM” , And endOfWeek is “12:00 AM on October 1, 2017”, which is the correct value for this week.

Do not use print(Date()) to check the correctness of the Date object, because as As you can see, it will produce unexpected results due to the use of the basic DateFormatters.

Leave a Comment

Your email address will not be published.