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
Related
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)
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)
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)
This question already has answers here:
Displaying the Day Of The Week From Date Picker
(2 answers)
Closed 7 years ago.
I'm an absolutely coding newb and I want to create a school planner app. I have the general nuts and bolts sorted - but I want to create a function where a label displays the day of the week, which a button would then interpret and would then send the user to the appropriate View Controller with that day's timetable in.
I've tried YouTube and of course here, and I can make no sense of it at all. Can someone treat me like a little baby and explain it to me. The name of the label is DayLabel1 and I think I can connect it as an IBOutlet.
I make no sense out of this whatsoever, and if you can't help me, I am either
a) doomed
b) still doomed
This is not a duplicate as I'm not wanting a date picker. I'm a newb and just want a hand.
Thank you! :-)
[[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]] weekday];
This code gives you to number of day in week which start from Sunday (index of Sunday is 0). You can make an String enum to get string using this index.
Here is the Swift:
let calendar = NSCalendar.currentCalendar()
let date = NSDate()
let dateComponent = calendar.components(NSCalendarUnit.CalendarUnitWeekday, fromDate: date)
let weekday = dateComponent.weekday
This question already has answers here:
Number of days between two NSDates [duplicate]
(16 answers)
Relative string from NSDate
(5 answers)
Closed 8 years ago.
So I'm trying to get the amount of days a blog post was posted ago. The end result would be something like "posted X days ago."
After spending some time on the problem, I figured it out by getting the NSTimeInterval, dividing to get days and then rounding. Although I got the output I wanted, I feel I am doing it wrong or there is a much more straight forward way of doing it.
tempDate is a NSDate object of when the blog was posted.
NSTimeInterval timeSince = [tempDate timeIntervalSinceNow];
timeSince = timeSince/60/60/24*-1; // seconds to days
int daysSince = lroundf(timeSince);
you could use NSCalendar and NSDateComponent to get the number of days (and you don't even need to convert your tempDate)... See the good answer given here Number of days between two NSDates