Add 1 Day to a Date [closed] - ios

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am creating an alarm clock app for IOS. Long story short, I have configured the DatePicker to display only hours and minutes.
My problem is that When the user inputs 6:00 AM, I want it to be for the following morning, where Xcode now is assuming it is the same day.
EX: The user goes to sleep at 10 pm on a Wednesday and sets his alarm for 6:00 AM Thursday. My app is assuming the 6:00 AM is meant for Wednesday. How can I fix this?

The most reliable way to get the next occurrence of a time is nextDate(after:matching:matchingPolicy: of Calendar because it considers also daylight saving changes.
assuming datePicker is the NSDatePicker instance:
let date = datePicker.date
let calendar = Calendar.current
// get hour and minute components of the given date
let components = calendar.dateComponents([.hour, .minute], from: date)
// calculate the next occurrence of the date components from now
let nextOccurrence = calendar.nextDate(after: Date(), matching: components, matchingPolicy: .nextTime)

I like using the AFDateHelper library personally.
import AFDateHelper
let tomorrowDate = dateFromPicker.adjust(.day, offset: 1)

Related

Swift: first day of the week in app doesn't change after I change it in Settings

In my app I need to group different items based on their date, so I need a method to get date of the beginning of the week, and also a method to find out if two dates are in the same week. However, I get unexpected problems trying to implement this.
I use this code to get the first day of the week:
return Calendar.current.date(from: Calendar.current.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
I use Calendar.current, but it returns the same day no matter which day is chosen as the beginning of the week in iOS Settings.
To check if two dates are in the same week I use this code:
func isInSameWeek(date: Date) -> Bool { isEqual(to: date, toGranularity: .weekOfYear) }
Still, Sunday and Monday dates are always considered not in the same week no matter which day is chosen as the beginning of the week in iOS Settings.
How do I fix this? Or maybe it is a normal behavior and this code will work correctly for users in different regions?
You are changing the settings of the Calendar App not the iOS current calendar itself. You need to provide your own app settings to be changed by the user.
var calendar = Calendar.current
calendar.firstWeekday = 2
calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date()))?.description(with: .current) // "Monday, September 14, 2020 at 12:00:00 AM Brasilia Standard Time"

How to convert current year month and day into minutes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i need a variable which is contains the actual year month and day in minutes
that should be around 1061841278
var plus = calendar.dateComponents([.year, .month, .day], from: Current).minute
i wrote this but it didn't work...
Your question is tricky because it looks like your reference date is before the adoption of the Gregorian calendar. So you have to think very hard about what date you're counting from.
But let's assume you mean to count from midnight on January 1 in the year 1 on the Gregorian calendar in UTC. That's kind of a nonsense thing to say, because neither the Gregorian calendar nor UTC existed at that time. But we can extrapolate, and this will show how to approach the problem for other dates.
First, you need a calendar and a timezone to perform the calculations.
let gregorian = Calendar(identifier: .gregorian)
let utc = TimeZone(abbreviation: "UTC")!
Then, you need a reference date:
let ref = gregorian.date(from: DateComponents(timeZone: utc, year: 1, month: 1, day: 1))!
And then you can perform the calculation in minutes:
let diff = gregorian.dateComponents([.minute], from: ref, to: Date()).minute
When I did this, the answer was 1061841271, which is pretty close to your expectations.
Be very careful with creating reference dates in this era. You will want to read the Historical Dates section of the Date and Time Programming Guide to ensure you're dealing with the calendars correctly. In particular, there is no year 0, and Calendar skips 10 days in 1582 due to the Julian/Gregorian adjustment.

How to subtract ten minutes from a time picked from time picker? [duplicate]

This question already has answers here:
Adding and Subtracting times in Swift
(2 answers)
Closed 3 years ago.
I have a time picker in which the user picks the time and I am sending a notification to user ten minutes before or after the time user picked.Suppose if the user picks time as 11:00 AM then I want the notification to trigger at 10:55 AM. I am unable to subtract and add ten minutes to the time. As I am new to this Please help..I have spent a lot of time on it still not able to solve this. Any help is appreciated
You could subtract minutes using the Calendar :
Calendar.current.date(byAdding: .minute, value: -10, to: yourDate)
Use Calendar of iOS.
let beforeThreeMinutes = Calendar.current.date(byAdding: .minute, value: -3, to: datePicker.date)

Swift 4 days until date [duplicate]

This question already has answers here:
Getting the difference between two Dates (months/days/hours/minutes/seconds) in Swift
(20 answers)
Closed 5 years ago.
I'm struggling in calculation the days between some dates .
I want to calculate the days until new year or the minutes.
I tried a way which I saw on another post here by using dateComponents but I wasn't able to calculate.
If some one can help I would be realy happy.
Thank You !
It would be helpful to see what you've tried to give some advice, but nonetheless, you can use NSCalendar's components to accomplish this.
let calendar = Calendar.current
let startOfDay1 = calendar.startOfDay(for: date1)
let startOfDay2 = calendar.startOfDay(for: date2)
let components = calendar.dateComponents([.day], from: startOfDay1, to: startOfDay2)
You can customize that above to get more specific minutes from your date object.
https://developer.apple.com/documentation/foundation/nscalendar/1407925-components
Edit: For Swift 4, you don't need to bridge. You can use Calendar directly (edited code above). https://developer.apple.com/documentation/foundation/calendar/2293176-datecomponents

Swift Date object with different format [duplicate]

This question already has answers here:
Comparing NSDates without time component
(17 answers)
Closed 5 years ago.
I've got a Date object that holds year/month/day and hour/minute/second data. I need to drop the hour/minute/second part as it's making comparing days problematic.
The issue is that the only obvious way I see of doing it is turning the day into a String and then using a DateFormatter with yyyy/mm/dd format to turn it BACK into a Date. This seems like a waste, is it really the only way?
Thanks a lot.
Never convert dates to String and back to Date.
You are looking for the startOfDay function of Calendar:
let date = Date()
let startOfDay = Calendar.current.startOfDay(for: date)

Resources