0

Let me give an example of what am trying to achieve: Say i am in Hong Kong [HKT] and on my app all i need to know if the current time in America/Sitka is after evening 5:30PM or not. [i have to execute a piece of code after certain time and will continue to do that till next morning sometime.]

Now in my app i have the information for home time zone and the remote time zone . (have NSTimeZone object for the same)

I can print the time for both if i want, using this code.

            var nTZ:NSTimeZone = NSTimeZone(name: "America/Sitka")

            var dateFormatter: NSDateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "hh:mm a"
            dateFormatter.timeZone = NSTimeZone(abbreviation: nTZ.abbreviation!)
            var remoteTime: NSSTring = dateFormatter.stringFromDate(NSDate())

Now the problem is that dateformatter converts date into string format. And how do i figure out if currently the time in the remote place is after say evening 5:30PM. Basically how to compare the time?

One thing which i also tried was reversing the time to NSDate. ie. take the String date and convert back to NSDate. Idea was if i can convert it to a Date format i will try some kind of comparison (which i have yet to figure out.) //Below code is continuation of previous code

    var dateFormatter2: NSDateFormatter = NSDateFormatter()
    dateFormatter2.dateFormat = "HH:mm a"
    var tempDateX:NSDate = dateFormatter2.dateFromString(remoteTime)!

    println(tempDateX)

This do show some value but that doesn't matches the original date. Looks like it prints the time minus by current GMT offset. Not sure why.

Please help me with this problem. Appreciate your help.

KD.
  • 2,015
  • 3
  • 28
  • 59
  • My first option gives me a string not a date. 2nd option gives me a date but not the correct date. If my 2nd code is able to give me the right information i might be able to use this – KD. Oct 11 '14 at 17:57
  • Why do you say in the 2nd case the date is incorrect? – zaph Oct 11 '14 at 18:16

2 Answers2

2

Example:

var localTimeString = "5:30"
var localTZ = NSTimeZone.localTimeZone()

var remoteTimeString = "10:24"
var remoteTZ:NSTimeZone? = NSTimeZone(name: "America/Sitka")

var dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm"

dateFormatter.timeZone = localTZ
var localDate:NSDate = dateFormatter.dateFromString(localTimeString)!
println("localDate: \(localDate)") // localDate: 2000-01-01 10:30:00 +0000

dateFormatter.timeZone = remoteTZ
var remoteDate:NSDate = dateFormatter.dateFromString(remoteTimeString)!
println("remoteDate: \(remoteDate)") // remoteDate: 2000-01-01 19:24:00 +0000

var isLater:NSComparisonResult = localDate.compare(remoteDate)
if isLater == .OrderedAscending {
    println("OrderedAscending")
}
else {
    println("OrderedDecending")
}
zaph
  • 111,848
  • 21
  • 189
  • 228
  • Was able to tweak this code and make it work. NSComparisonResult is all i had to use. Appreciate your help. – KD. Oct 12 '14 at 08:16
0

Your task becomes easier if you compute the remote time as "HHmm" using the 24-hour "HH" format, because then you can simply compare the resulting string with "1730":

let dateformatter = NSDateFormatter()
dateformatter.timeZone = NSTimeZone(name: "America/Sitka")
dateformatter.dateFormat = "HHmm"
dateformatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")

let remoteTime = dateformatter.stringFromDate(NSDate())
if (remoteTime > "1730") {
    println("After 5:30 pm in Sitka")
}

(The Posix locale is set to ensure that the conversion is done independent from the device settings, compare What is the best way to deal with the NSDateFormatter locale "feechur"?.)

Alternatively, you can compute the remote hours and minutes numerically using NSDateComponents:

let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone(name: "America/Sitka")
let comp = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: NSDate())

// Or: if comp.hour * 100 + comp.minute > 17 * 100 + 30 { ...
if comp.hour > 17 || (comp.hour == 17 && comp.minute > 30) {
    println("After 5:30 pm in Sitka")
}
Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • @Zaph: As I understand the question (and I might be wrong of course), OP wants to check if the current "local wall clock time" at the remote location is after 17:30, and the above code should do exactly that. – Martin R Oct 11 '14 at 19:35
  • OK, I agree with your interpretation of the question. The title seems to imply a somewhat different question. – zaph Oct 11 '14 at 19:38