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

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.

Related

Setting only specific months in UIDatePicker Swift 5

In my project i want to apply condition like, user can select date only 3months from current date.
I.e i want to display a date picker containing Month and Day with range of 90 days.
Eg. November is going on so only, November December January should be displayed in Date picker for picking.
How can i achieve that??
You can try something like this:
datePicker.maximumDate = Calendar.current.date(byAdding: .month, value: 3, to: Date())
UIDatePicker has minimumDate and maximumDate properties. Use those to set the range of dates you want to allow the user to enter.
Edit:
#wonder posted code to calculate a date 3 months in the future:
futureDate = Calendar.current.date(byAdding: .month, value: 3, to: Date())
Alternately, you could add 90 days to the current date if that's what you want:
futureDate = Calendar.current.date(byAdding: .day, value: 90, to: Date())
Set the datePickerMode to .date to have the user choose only dates, not times.
I don't know how to remove the year selector, although if you select a date range of 90 days that all fall in the same year, they won't be able to select a year other than the current year.
(Note that starting in October, a date range of 90 days includes dates in 2 different years, so you would need the year anyway.)
As wonder said in a comment, you could create your own month and day picker using a UIPickerView, but you would need to add logic to change the number of days based on the month, and exclude days of the current month that are in the past.

How calculate days to person birthday with Swift?

I have a Date variable with a person's date birthday. I would like to know how many days remains before this person next birthday. It should be calculated from today date to current year birthday date.
How can this be done with Swift? Also it will be great to consider February 29 in leap years.
To the guys who tried to close this: This is about birthday which has totally different rules from days.
Birthdays are complicated. Your birthday was the date of the moment when you were born, in the timezone where you were born. Considering that Samoa = UTC+14 and Baker Island = UTC-12, it is possible that people born at the same moment in time have birthdays that are two days apart.
So to store somebody's birthday, not the moment of birth, you either store year/month/day, or if you want to store it as a point in time, you store the beginning of that day in UTC, with the understanding that this is to specify a day, and must not be converted to local time.
Now when does your birthday repeat? If the person is born on D/M/Y and D/M is not February 29th, then the next birthday is either D/M/current year or D/M/next year. It is D/M/current year if that date is in the future, otherwise D/M/next year.
If the person is born on February 29th, then you have to determine when officially the next birthday is if the year is not a leap year - this will be February 28th or March 1st, depending on which rules apply.
We also need to clarify what "number of days" means. If my birthday is on April 1st, and now it is March 31st, one second to midnight, my birthday will be one second from now. However, I will assume that the result is supposed to be "one day from now".
So here is the algorithm:
Step 1: Find day/month/year when the person was born.
Step 2: Determine the current time, and convert it to local day/month/year. Determine the current time only once to avoid problems if this calculation is done nanoseconds before midnight.
Step 3: Determine the year when the birthday repeats: If day/month of birthday is greater than current day/month, then the year when the birthday repeats is the current year, otherwise the next year. This is also correct if the birthday was on Feb. 29th.
Step 4: Determine the day/month when the birthday repeats: This is the same as the day/month of the birthday, unless the birthday was on Feb. 29th and the year when the birthday repeats is not a leap year. In that case, the birthday repeats on Feb 28th or March 1st, depending on which rules you decide to apply.
Step 5: Convert the current day/month/year + 12 hours to UTC. Convert the date when the birthday repeats + 12 hours to UTC. Calculate the difference in seconds (which the OS should do for you). Divide by 86,400, then round to the nearest integer. The "+12 hours" and "round to nearest integer" make sure that you have no problems with daylight savings time, leap seconds etc.
Writing this code in Swift or any other language should be no problem.
It depends on what you are looking to use the days value for but here is a small function that will return a Double describing the amount of days until a given Date. Martin R gave a really good answer here and my answer is mainly based on theirs with a little bit of documentation added.
/// This function takes a `Date` parameter and returns an `Int` describing how many days away the `Date` is into the future (accounting for leap years by making 2/29 dates land on 3/1 in non-leap years).
/// - Parameter date: The `Date` object to be evaluated.
func daysUntil(birthday: Date) -> Int {
let cal = Calendar.current
let today = cal.startOfDay(for: Date())
let date = cal.startOfDay(for: birthday)
let components = cal.dateComponents([.day, .month], from: date)
let nextDate = cal.nextDate(after: today, matching: components, matchingPolicy: .nextTimePreservingSmallerComponents)
return cal.dateComponents([.day], from: today, to: nextDate ?? today).day ?? 0
}

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)

Set calendar type on whole application

I've an application that is working fine with Gregorian calendar type. All API info for date is in Gregorian calendar type. But if users have Buddhist calendar I have crashes in some places. For example:
func days(from date: Date) -> Int {
let beginningOfDay = date.beginningOfDay ?? date
return Int(timeIntervalSince1970 - beginningOfDay.timeIntervalSince1970) / Int(TimeInterval.day)
}
How can I ignore user settings for calendar and force date to be in Gregorian calendar in whole application? Is it possible?
I've just founded converters, but I don't want to convert, I just want to force Gregorian calendar on whole app.
Thanks
This looks like you've added a lot of extensions on Date that don't belong there. Date is a point in time. In order to talk about "days" you should be calling Calendar and DateComponents methods. This function isn't quite correct anyway. It can be off by a day depending on DST changes. You can't assume that a day is 24 hours long; some are 25 hours, and some are 23 hours.
The code you wanted was:
let calendar = Calendar(identifier: .gregorian)
calendar.dateComponents([.day], from: d1, to: d2).day!
Likely somewhere in your extensions you have Calendar.current. That means "the current user calendar." There's no way to tell the system "even when I explicitly ask for the current user calendar, please give me something else." Look for the code that uses Calendar.current and replace it with Calendar(identifier: .gregorian) if that's what you mean.

Add 1 Day to a Date [closed]

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)

Resources