2016-07-28 7 views
5

dateとcurrentDateを比較するとタイトルに非常に奇妙な反応があります。 次のようなことが起こります: 私はサーバーに照会して日付を取ってから印刷しますので、正しいと思います(そしてすべては問題ありません)。 その後、3時間前に表示されているcurrentDateを表示します。 さて、私はそれを修正し、私は言っている。 でも!私がそれらを比較しようとしているときは、20:59以前の日付しか取らない。日付をcurrentDateと比較するときの不思議な反応Swift

これは私のコード(//dateevent are the dates that I recover from server

if dateevent.earlierDate(self.currentDate).isEqualToDate(self.currentDate){ 
    print("All dates \(dateevent)") 
    if NSCalendar.currentCalendar().isDate(dateevent, equalToDate: self.currentDate, toUnitGranularity: .Day){ 
    print("here is what it passed from server \(dateevent)") 
    print("here is the current date \(self.currentDate)") 
         } 

        } 

あるこの私の出力である

All dates 2016-07-28 19:00:00 +0000 
here is what it passed from server 2016-07-28 19:00:00 +0000 
here is the current date 2016-07-28 13:43:51 +0000 

All dates 2016-07-28 19:00:00 +0000 
here is what it passed from server 2016-07-28 19:00:00 +0000 
here is the current date 2016-07-28 13:43:51 +0000 

All dates 2016-07-28 21:00:00 +0000 
All dates 2016-07-28 21:00:00 +0000 
All dates 2016-07-28 23:30:00 +0000 
All dates 2016-07-29 21:00:00 +0000 
All dates 2016-07-29 22:30:00 +0000 
All dates 2016-07-29 23:00:00 +0000 
All dates 2016-07-29 23:00:00 +0000 
All dates 2016-07-29 23:30:00 +0000 
All dates 2016-07-30 21:00:00 +0000 
+0

を、あなたは何ですか? – Larme

+0

私はギリシャにいて、UTC + 02:00 –

+0

を比較する前に 'currentDate'と' dateevent'を表示しています。 'self.currentDate'が変更されたことは明らかです。あなたの最初の行は、 'dateevent.compare(self.currentDate)== OrderedDescending'として表現されやすくなりました。 – fishinear

答えて

0

私はついに私の質問の答えを見つけました!

私は私がやったことはこれでそれをhere

が見つかりました:

:私はこれを入れて、私の文で

utcCalendar!.timeZone = NSTimeZone(abbreviation: "UTC")! 

とfinnaly:

let utcCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 

内部のviewDidLoad I PUこの

if self.utcCalendar!.isDateなど...

ありがとうございました。

EDIT!

私もこのようたDateFormatter変更する必要が日付印刷したい場合は:どのような時間帯で

dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")! 
1

ギリシャの夏は、実際にはUTCとの3時間の時差があります。したがって、UTC 2016-07-28 21:00:00 +0000以降の時間は実際にギリシャの翌日になります。だからあなたはそれを地方(ギリシャ)カレンダーによると比較すると、

NSCalendar.currentCalendar().isDate(dateevent, equalToDate: self.currentDate, toUnitGranularity: .Day) 

となります。

あなたの代わりにUTC日数に応じて比較したい場合は、UTCにカレンダーオブジェクトのタイムゾーンを設定します。

NSCalendar *calendar = NSCalender.currentCalendar(); 
calendar.timeZone = NSTimeZone.timeZoneForSecondsFromGMT(0); 
if (calendar.isDate(dateevent, equalToDate: self.currentDate, toUnitGranularity: .Day)) ... 

あなたには、いくつかの他の時間帯に応じて比較したい場合、あなたは明らかに設定する必要がありますtimeZoneとは異なります。

NSDateFormatter().stringFromDate(dateevent) 
+0

と私は何をしていますか? –

+0

答えを更新しました – fishinear

+0

大丈夫ですよ。ありがとうございます。 –

2

:あなたはローカルのタイムゾーンに応じた日数を比較するとOKですが、日付がUTCに印刷されていることを簡単に混乱している場合


は、その文字列に変換するために、次を使用します私はあなたがサーバー側で使うべきタイムゾーンを議論するつもりはありませんが、最も便利で一貫した方法はUTCです。サーバー上ですでにUTCタイムゾーンを使用しているので、サーバーに日付と時刻を格納するときにこれを考慮する必要があります。私がこれを意味するのは、もしあなたが例えば2016-07-28 21:00:00 +0000、必ずしもあなたの所在地で2016-07-28 21:00:00に翻訳されるとは限りません。

は、次を参照してください。

let time = NSDate()  // Create an object for the current time. 

print(time, "\n")   // Sample output: 
           2016-07-28 15:23:02 +0000 

そのままプリントアウトtimeオブジェクトはUTCでの現在の時刻を出力します。参考までに、私のローカルタイムゾーンもUTC + 3になるため、現地時間は2016-07-28 18:23:02 +0300です。

のは、次の文字列形式でいくつかの日付を見てみましょう:

let strings = [ 
    "2016-07-28 19:00:00 +0000", 
    "2016-07-28 20:00:00 +0000", 
    "2016-07-28 20:59:59 +0000", // Second before midnight, UTC+3. 
    "2016-07-28 21:00:00 +0000", // Midnight in UTC+3. 
    "2016-07-28 22:00:00 +0000", 
    "2016-07-28 23:30:00 +0000", 
    "2016-07-28 23:59:59 +0000", // Second before midnight, UTC. 
    "2016-07-29 00:00:00 +0000" // Midnight in UTC. 
] 

そして今、のは再び、UTCタイムゾーンに残りますNSDateオブジェクト、にこれらの文字列を変換してみましょう:

var dates: [NSDate] = [] 

for string in strings { 
    let formatter = NSDateFormatter() 
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z" 
    guard let date = formatter.dateFromString(string) else { continue } 
    dates.append(date) 
} 

次に、NSDateオブジェクトをローカルフォーマットの文字列に変換します。これはおそらくあなたの混乱の原因となります。

for date in dates { 
    print("Current time in UTC: ", time) 
    print("Date in UTC:   ", date) 

    let formatter = NSDateFormatter() 
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" 
    formatter.timeZone = NSTimeZone.localTimeZone() 
    print("Current local time: ", formatter.stringFromDate(time)) 
    print("Date in local time: ", formatter.stringFromDate(date)) 
} 

// Sample output for the date 2016-07-28 19:00:00 +0000: 
// Current time in UTC: 2016-07-28 15:23:02 +0000 
// Date in UTC:   2016-07-28 19:00:00 +0000 
// Current local time: 2016-07-28 18:23:02 
// Date in local time: 2016-07-28 22:00:00 
+0

非常に良い説明私は上記の答えとしてこれを使用しようとし、私はあなたに私の結果を教えてくれてありがとう –

関連する問題