Convert Double to NSDate time, for querying on Parse - swift

I have two Doubles, and need to query for NSDate's with a timeinterval that matches the two doubles.. How can I convert a Double to a NSDate with time matching my Double? :) If it's possible at all..
Edit: Sorry, I was in a bit of a hurry - my apologies! The Doubles I have, represent a time of the day, like 8.00 or 17.00 .. So I need to convert that Double, to a time in an NSDate, If that makes sense :)

Simple use timeIntervalSince1970 and pass double value as parameter.
let date = NSDate(timeIntervalSince1970: doubleValue)

I found the solution with a bit of hints from your answers. Here's what I ended up with and it works perfectly:
let date = NSDate()
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let newDate = cal.startOfDayForDate(date)
let secondsSince1970ToYesterdayAt23 = newDate.timeIntervalSince1970
let secondsInOneHour = 3600.0
let secondsSince1970FromMidnight = secondsSince1970ToYesterdayAt23 + secondsInOneHour
let secondsFromMidnightToStartTime = (secondsInOneHour * (slider?.lowerValue)!) + secondsSince1970FromMidnight
let dateWithStartTime = NSDate(timeIntervalSince1970: secondsFromMidnightToStartTime)
let secondsFromMidnightToEndTime = (secondsInOneHour * (slider?.upperValue)!) + secondsSince1970FromMidnight
let dateWithEndTime = NSDate(timeIntervalSince1970: secondsFromMidnightToEndTime)
print("Start \(dateWithStartTime)") // Returning 2017-03-18 08:00:00 +0000
print("End: \(dateWithEndTime)") // Returning 2017-03-18 17:00:00 +0000

Related

DateIntervalFormatter: string of format "m:ss"

I'd like to get a string of format "m:ss" out of two dates.
E.g.: "0:27" for 27 seconds difference and "1:30" for 90 seconds difference between dates.
Here's the code I'm using:
import Foundation
let formatter = DateIntervalFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .none
formatter.dateTemplate = "m:ss"
let startDate = Date()
let endDate = Date(timeInterval: 1, since: startDate)
let outputString = formatter.string(from: startDate, to: endDate)
print(outputString) //16:12 – 16:13 ???
// This is correct, but it doesn't actually calculate the interval.
But I'm getting just two dates printed out with a dash.
How can I actually make the DateIntervalFormatter to calculate the difference as I want?
The code is almost 1:1 sample from the Apple documentation but with the custom dateTemplate: https://developer.apple.com/documentation/foundation/dateintervalformatter
It seems that you actually want DateComponentsFormatter
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.minute, .second]
formatter.zeroFormattingBehavior = .pad
let startDate = Date()
let endDate = Date(timeInterval: 129, since: startDate)
let outputString = formatter.string(from: startDate, to: endDate)!
print(outputString)
to remove the leading zero if the minutes are < 10 you could use Regular Expression
print(outputString.replacingOccurrences(of: "^0(\\d)", with: "$1", options: .regularExpression))
I created this solution which doesn't involve the DateIntervalFormatter:
import Foundation
let minutes = 2
let seconds = 9
let formatted = String(format: "%01d:%02d", minutes, seconds)
print(formatted) // 2:09
Looks like what DateIntervalFormatter does is just applying a standard Date->String conversion to both of the dates and adds a dash between them.

Is there a more efficient way to combine a date and time in swift 5? [duplicate]

This question already has an answer here:
How to combine two strings (date & time) into a new date in Swift 3
(1 answer)
Closed 2 years ago.
I have function which receives 2 strings the first is a date "y-M-d" and the second a time "HH:mm". I then combine them using the following code.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "y-M-d"
let date = dateFormatter.date(from: dateStr)!
dateFormatter.dateFormat = "HH:mm"
let time = dateFormatter.date(from: timeStr)!
let calendar = Calendar.init(identifier: .iso8601)
let components = NSDateComponents()
components.day = calendar.component(.day, from: date) //split from date above
components.month = calendar.component(.month, from: date)
components.year = calendar.component(.year, from: date)
components.hour = calendar.component(.hour, from: time) //split from time above
components.minute = calendar.component(.minute, from: time)
let newDate = calendar.date(from: components as DateComponents)
The code all works fine and is doing what I want it to. However, I was wondering if anyone can suggest a slicker way of doing it, using less lines of code?
You can join the date & time strings and parse them in one go:
let dateStr = "2020-03-12"
let timeStr = "15:35"
let df = DateFormatter()
df.dateFormat = "y-M-d HH:mm"
let date = df.date(from: dateStr + " " + timeStr)
// prints: 2020-03-12 13:35:00 +0000 (my machine is GMT+2)
Edit: As Leo Dabus said in the comments, a more appropriate format for the provided strings should be yyyy-MM-dd HH:mm (just kept the provided format from the question). The spirit of the answer was not to propose a format but to provide a way to avoid parsing date/time separately.
Simply get a combined String using date and time. Then use that String to get the Date instance from it.
func getDate(d: String, t: String) -> Date? {
let str = d + t
let formatter = DateFormatter()
formatter.dateFormat = "y-M-dHH:mm"
let date = formatter.date(from: str)
return date
}

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.

Using DateFormatter to parse an esoteric date string

I would like to use Foundation's DateFormatter to parse a datestring of the rather weird format /Date(1488335133000+0100)/ (representing 2017-03-01 03:25:33). As far as I can tell this describes the date as milliseconds since 1970 with a timezone of GMT+1 specified as well.
I can't however find a way to specify a format string for milliseconds or seconds as a unix timestamp. Is this even possible?
In case that's not possible, what would be the best option for parsing this date correctly? I'm currently resorting to picking apart the string until I have the milliseconds and timezone, dividing the milliseconds by 1000 and creating a new date object via Date(timeIntervalSince1970: seconds). Not quite sure how the timezone is supposed to play into this though.
DateFormatter can't handle this. Use NSRegularExpression to pick apart the components:
let str = "/Date(1488335133000+0100)/"
let regex = try! NSRegularExpression(pattern: "/Date\\((\\d+)(\\+|-)(\\d{2})(\\d{2})\\)/", options: [])
if let match = regex.firstMatch(in: str, options: [], range: NSMakeRange(0, str.characters.count)) {
let nsstr = str as NSString
let millisecond = Double(nsstr.substring(with: match.rangeAt(1)))!
let sign = nsstr.substring(with: match.rangeAt(2))
let hour = Double(nsstr.substring(with: match.rangeAt(3)))!
let minute = Double(nsstr.substring(with: match.rangeAt(4)))!
let offset = (sign == "+" ? 1 : -1) * (hour * 3600.0 + minute * 60.0)
let date = Date(timeIntervalSince1970: millisecond / 1000 + offset)
print(date)
}

Swift: NSDate minus NSDate in hours

I have a model in Swift with an NSDate field named expirationDate and I want to calculate the hours remaining before expiration based on the current date.
Is there an easy way to do this with existing NSDate functionality?
Can't check now, but should be something like
expirationDate.timeIntervalSinceNow / 3600
Just find the number of seconds (NSTimeInterval) between the two dates ('now' and your expiration date) and divide by 60*60 = 3600:
let secondsUntilExpiration = expirationDate.timeIntervalSinceDate(NSDate());
let hoursUntilExpiration = secondsUntilExpiration / 3600
For example:
7> let now = NSDate()
now: NSDate = 2016-02-01 03:44:06 UTC
8> let expirationDate = now.dateByAddingTimeInterval(60*60*10) // ten hours from now
expirationDate: NSDate = 2016-02-01 13:44:06 UTC
9> let secondsUntilExpiration = expirationDate.timeIntervalSinceDate(NSDate());
secondsUntilExpiration: NSTimeInterval = 35991.422316968441
10> let hoursUntilExpiration = secondsUntilExpiration / 3600
hoursUntilExpiration: Double = 9.9976173102690122
// Slightly less than the 10 hours above because of the time it took me to type.