Time and Date Calculations - swift

Using date format "EEEE:dd:MMM:HH:mm" returns the correct date and time, however, the day of the week is incorrect. For example a 750 nautical mile voyage conducted at 7.5 knots will take 100 hours. If I use my code to calculate the arrival time using a start of, say, Friday 1 Nov at 12:00 it returns Sunday 5 Nov at 16:00. Time and date are correct but day of the week is not. Should be Tuesday.
#IBAction func Calculate(_ sender: UIButton) {
let userCalendar = Calendar.current
let dateMakerFormatter = DateFormatter()
dateMakerFormatter.dateFormat = "EEEE:dd:MMM:HH:mm"
let distance = (Distance.text! as NSString).floatValue
let speed = (GndSpd.text! as NSString).floatValue
let calcDT = ((distance / speed) * 3600)
if var date = dateMakerFormatter.date(from: (DTG.text!)) {
date = dateMakerFormatter.date(from: (DTG.text!))!
var timeAdj = DateComponents()
timeAdj.second = Int(calcDT)
if var adj = userCalendar.date(byAdding: timeAdj, to: date) {
adj = userCalendar.date(byAdding: timeAdj, to: date)!
CalcDTG.text = dateMakerFormatter.string(from: adj)
}
}
}

You should use d for Day, not D
dateMakerFormatter.dateFormat = "EEEE:dd:MMM:HH:mm"
DateFormatter

You can't say the day of the week is incorrect when you're not giving a year.
The date formatter seems to ignore the day of the week when creating a date:
let dateMakerFormatter = DateFormatter()
dateMakerFormatter.dateFormat = "EEEE:dd:MMM:HH:mm"
let date = dateMakerFormatter.date(from: "Friday:01:Nov:12:00")!
print(date) -> 2000-11-01 12:00:00 +0000
print(dateMakerFormatter.string(from: date)) -> Wednesday:01:Nov:12:00
Hey presto, you're now in the year 2000, where 5 November did fall on a Sunday.
The important takeaway you need is that you should never, ever, ever, use strings to pass around date values in your code. Use Date. If you're getting a date from an API response, change it to a date on ingestion. If you're getting one from user entry, use a date picker or other control. If you're getting one from a string the user is typing in, I'd politely suggest you're making unnecessary work for yourself, but do make sure you fill in all the details the user doesn't give you.

Related

Swift get the remaining seconds of time on target string date

The current date time today was May 9, 2020 10:03 PM, and I have a target string date with the value of 2020-05-09 22:07:30 with the format of yyyy-MM-dd HH:mm:ss.
How can I get the remaining date from that 2 date and print the value with the string of 04:30 as the range of those 2 dates are 4 minutes and 30 seconds
What I can only do is convert the milliseconds to time format like
func msToTime(ms: Int) {
let seconds = ms % 60
let minutes = ms / 60000
return String(format: "%0.2d:%0.2d",minutes,seconds)
}
Output 04:30
But I don't know how to get the range of milliseconds from today's date time to target's date time.
Or if there's any other easier way to do it?
You can use Calendar and DateComponents to easily calculate differences between dates in whatever units you desire. For example, this gets the difference in minutes and seconds:
let dateformatter = DateFormatter()
dateformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateformatter.date(from: "2020-05-09 22:07:30")!
let now = Date()
let components = Calendar.current.dateComponents([.minute, .second], from: now, to: date)
print("difference: \(components.minute!):\(components.second!)")
A straightforward way, with no calculation of any kind needed:
let d1 = Date()
let s = "2020-05-09 22:07:30"
let f = DateFormatter()
f.dateFormat = "yyyy-MM-dd HH:mm:ss"
f.timeZone = TimeZone.current // or whatever
if let d2 = f.date(from: s) {
let f = DateComponentsFormatter()
f.unitsStyle = .positional
let result = f.string(from: d1, to: d2)
}
If you don't like the resulting string format of result, you can eliminate pieces of it. — However, note that this works only because no full days are involved. It isn't clear what the range of possible inputs might be or what the desired output would be if the second date were three months into the future, for example.

Convert hours form 12 hour time to 24 hour time Swift and save as an integer

I want to convert my variable for hours which is an integer into a 24 hour time system (for example, if it is 01:05:13 PM, hours will be saved as 13, minutes will be saved as 5, and seconds will be saved as 13) so that I can use it for some math later in my code to fid some differences on a scheduling app I am working on. This is my first app and I couldn't find an answer to this anywhere else so thanks for your help! Another way this code could work is getting the amount in seconds since the day has begun, if anyone knows how to do that, it would be greatly appreciated!
This is my function for getting the time and saving it as three different integers for hours, seconds, and minutes:
#IBAction func setTime() {
var date = NSDate()
//pickTimes()
var calendar = NSCalendar.current
calendar.timeZone = TimeZone(identifier: "UTC")!
var currentHour = calendar.component(.hour, from: date as Date) + 5
let currentMinutes = calendar.component(.minute, from: date as Date)
let currentSeconds = calendar.component(.second, from: date as Date)
timeText.text = ("\(currentHour):\(currentMinutes):\(currentSeconds)")
}
calendar.component(.hour, from: someDate) already gives you the time of day in 24 hour time so there's nothing else to do to solve your question.
Not sure why you are adding 5 to the hour. You set the timezone to UTC so the date will be treated as the UTC timezone. Then you add 5 to that result. That's kind of strange. If you just want the current hour in the user's locale timezone, don't change the calendar's timezone and don't add 5 to the hour.
Don't use NSDate or NSCalendar. This is Swift. Use Date and Calendar.
Updated code:
#IBAction func setTime() {
var date = Date()
//pickTimes()
var calendar = Calendar.current
var currentHour = calendar.component(.hour, from: date)
let currentMinutes = calendar.component(.minute, from: date)
let currentSeconds = calendar.component(.second, from: date)
timeText.text = ("\(currentHour):\(currentMinutes):\(currentSeconds)")
}
But it would be simpler to use a DateFormatter and set the timeStyle to .medium or maybe .long and format Date() into a string. This will give a properly localized time string.

How to display only the hours when minutes are zero with date formatter?

Am currently using the format MM-dd-yyyy HH:mm to display the time stamp. Is it possible to drop the minutes if the time does not have any minute component ?
Like, instead of 3:00 PM is it possible to display only 3 PM ?
EDIT:
func formatDate(_ dateFormat: String, timeInterval: TimeInterval) -> String {
let date = Date(timeIntervalSince1970: timeInterval)
let formatter = DateFormatter()
formatter.dateFormat = dateFormat
return formatter.string(from: date)
}
let formattedDate = formatDate("MM-dd-yyyy HH:mm", timeInterval: 1520422200)
print(formattedDate)
Option 1
Replace any with zero minutes on return string.
return formatter.string(from: date).replacingOccurrences(of: ":00", with: "")
Option 2
Determine if there are minutes, and adjust date format.
if (Int(timeInterval) % 3600 == 0) {
let newFormat = dateFormat.replacingOccurrences(of: ":mm", with: "")
// ...
}
You could use DateComponents to determine what the .minute component is. If it's non-zero set the date format to include minutes, and otherwise set it to leave it out.
Note if this is for a user-facing string, use setLocalizedDateFormatFromTemplate and for hours use "j" so you can respect a user's setting for whether to display 24 hour time.
Note it is probably strange for most users to see 24 hour time without the minutes (e.g. "9pm" vs "21", the latter doesn't look right and probably would be better with ":00").

Test whether current time of day is between two TimeIntervals

I have 2 TimeIntervals, which just represent date-agnostic times of day (e.g. 8:00 AM and 5:00 PM). So 0 represents exactly midnight, in this case, and 29,040 represents 8:04 AM. I want to check if the phone's time of day is between the two TimeIntervals.
I found a few similar Stack Overflow questions, but none of them really dealt with TimeIntervals. It seems like just using start <= Date().timeIntervalSinceReferenceDate <= end or something wouldn't work, because it would return a huge value.
What's the best way to handle this type of situation in Swift 3?
Edit: To clarify, I don't need to worry about things like daylight savings. As an example, assume that the user only wants certain things in the app to happen between the hours of X and Y, where X and Y are given to me as TimeInterval values since midnight. So it should be sufficient to check if the phone's TimeInterval since midnight on a typical day is between X and Y before completing the action.
Date().timeIntervalSinceReferenceDate returns the number of seconds since Jan 1, 2000 so no doubt it's a huge number.
It's inadvisable to store time as seconds since midnight due to this naggy little thing called Daylight Saving Time. Every year, different countries do it on different days and on different hours. For example, even though Britain and France change their clock on the same day (March 26, 2017), one makes the shift from 1AM to 2AM, the other goes from 2AM to 3AM. That's very easy to make for a mess!
Use DateComponents instead:
let calendar = Calendar.current
let startTimeComponent = DateComponents(calendar: calendar, hour: 8)
let endTimeComponent = DateComponents(calendar: calendar, hour: 17, minute: 30)
let now = Date()
let startOfToday = calendar.startOfDay(for: now)
let startTime = calendar.date(byAdding: startTimeComponent, to: startOfToday)!
let endTime = calendar.date(byAdding: endTimeComponent, to: startOfToday)!
if startTime <= now && now <= endTime {
print("between 8 AM and 5:30 PM")
} else {
print("not between 8 AM and 5:30 PM")
}
I ended up using DateComponents to calculate a TimeInterval.
let components = Calendar.current.dateComponents(
[.hour, .minute, .second], from: Date())
guard let seconds = components.second,
let minutes = components.minute,
let hours = components.hour else
{
return false
}
let currentTime = Double(seconds + minutes * 60 + hours * 60 * 60)
return startTime <= currentTime && currentTime <= endTime

Finding the time between now and at specific time using swift 3

I am trying to find the time (in seconds) between now and a specific time during that day e.g 9:00 AM and if the time now is past this specified time in the day, I would want the time between now and that set time in the next day.
My ultimate goal is to run a function at every hour from 8:00 AM to 5:00 PM, while the application is not open/ in background. If there is a better way to do this without using a timer with an interval of 24 hours then I would really appreciate some suggestions.
For adding an hour to the current time. Use the below extension
extension Date {
func addHours(_ hours: Int) -> Date {
let seconds: TimeInterval = Double(hours) * 60 * 60
let newDate: Date = self.addingTimeInterval(seconds)
return newDate
}
}
call this function as Date().addHours(1)
To get specific time of current date
func getDateFromHour(hour: Int) -> Date {
let date = Date()
let calendar = Calendar.current
et componentsCurrent = calendar.dateComponents([.year, .month, .day], from: date)
var components = DateComponents()
components.hour = hour
components.minute = 0
components.second = 0
components.month = componentsCurrent.month
components.day = componentsCurrent.day
components.year = componentsCurrent.year
return calendar.date(from: components)!
}
Note: you should use 24-hour clock while passing the parameter hour.