2016-10-19 8 views
9

私は現在の時間をUTCでとり、それをnanaosecondsに入れてから、ナノ秒をとり、現地時間の日付に戻る必要があります。私はナノ秒の時間を取得してから日付の文字列に戻すことができますが、文字列を日付にすると時間が畳み込まれます。SwiftのDate to millisecondsとBackday

//Date to milliseconds 
    func currentTimeInMiliseconds() -> Int! { 
      let currentDate = NSDate() 
      let dateFormatter = DateFormatter() 
      dateFormatter.dateFormat = format 
      dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone! 
      let date = dateFormatter.date(from: dateFormatter.string(from: currentDate as Date)) 
      let nowDouble = date!.timeIntervalSince1970 
      return Int(nowDouble*1000) 
     } 

    //Milliseconds to date 
    extension Int { 
     func dateFromMilliseconds(format:String) -> Date { 
      let date : NSDate! = NSDate(timeIntervalSince1970:Double(self)/1000.0) 
      let dateFormatter = DateFormatter() 
      dateFormatter.dateFormat = format 
      dateFormatter.timeZone = TimeZone.current 
      let timeStamp = dateFormatter.string(from: date as Date) 

let formatter = DateFormatter() 
      formatter.dateFormat = format 
      return (formatter.date(from: timeStamp))! 
     } 
    } 

//タイムスタンプが正しいですが、返される日付は、私は、文字列を経由して、一見役に立たない変換を削除し、すべてのそれらのランダム!

+1

日付を文字列に変換して、日付を(currentTimeInMiliseconds()の中で)戻す目的は何ですか? – vadian

+0

私はすべての日付をミリ秒単位でストアしている会社 – user1079052

+1

しかし、二重変換の日付 - >文字列 - >日付は何ですか?また、コードには何も書かれていません。 – vadian

答えて

44

...

extension Date { 
    var millisecondsSince1970:Int { 
     return Int((self.timeIntervalSince1970 * 1000.0).rounded()) 
    } 

    init(milliseconds:Int) { 
     self = Date(timeIntervalSince1970: TimeInterval(milliseconds/1000)) 
    } 
} 


Date().millisecondsSince1970 // 1476889390939 
Date(milliseconds: 0) // "Dec 31, 1969, 4:00 PM" (PDT variant of 1970 UTC) 
+0

私はmillisecondsSince1970をUTCにし、init(ミリ秒:Int)を現地時間に戻す。 – user1079052

+2

日付オブジェクトは常に* UTCで表示されます。私の例のそれらの値はUTCです(記述メソッドがPDTでUTCエポックをどのようにレンダリングするかに注意してください)。 –

+2

Intの代わりにinit(ミリ秒:Double)を使う方が良いでしょう。そうしないと、変換時にミリ秒が失われます。 – dickyj

2
let dateTimeStamp = NSDate(timeIntervalSince1970:Double(currentTimeInMiliseconds())/1000) //UTC time //YOUR currentTimeInMiliseconds METHOD 
let dateFormatter = NSDateFormatter() 
dateFormatter.timeZone = NSTimeZone.localTimeZone() 
dateFormatter.dateFormat = "yyyy-MM-dd" 
dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle 
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle 


let strDateSelect = dateFormatter.stringFromDate(dateTimeStamp) 
print("Local Time", strDateSelect) //Local time 


let dateFormatter2 = NSDateFormatter() 
dateFormatter2.timeZone = NSTimeZone(name: "UTC") as NSTimeZone! 
dateFormatter2.dateFormat = "yyyy-MM-dd" 

let date3 = dateFormatter.dateFromString(strDateSelect) 
print("DATE",date3) 
4
//Date to milliseconds 
func currentTimeInMiliseconds() -> Int { 
    let currentDate = Date() 
    let since1970 = currentDate.timeIntervalSince1970 
    return Int(since1970 * 1000) 
} 

//Milliseconds to date 
extension Int { 
    func dateFromMilliseconds() -> Date { 
     return Date(timeIntervalSince1970: TimeInterval(self)/1000) 
    } 
} 

ではありません。あなたは文字列で何をやっている理由を私は理解していない

+0

これは素晴らしいですが、現地時間をUTCに変換するのにcurrentTimeInMillisecondsが必要で、UTCから現地時間に変換するdateFromMillisecondsが必要です – user1079052

+0

[タイムスタンプ**は** in UTC既に。](https://developer.apple.com/reference/foundation/nsdate/1416453-init)。 > '参照日(1970年1月1日00:00:00 ** UTC **)からの秒数' – user28434

10

@Travisソリューションが動作しますが、いくつかのケースで

var millisecondsSince1970:IntがCRASHアプリケーションの原因になりますと、

エラーあり

ダブルバリュー缶それはを発生した場合、結果はInt.maxより大きくなりますので、更新してくださいInt64のここ

とあなたの答えは、回答

extension Date { 
var millisecondsSince1970:Int64 { 
     return Int64((self.timeIntervalSince1970 * 1000.0).rounded()) 
     //RESOLVED CRASH HERE 
    } 

    init(milliseconds:Int) { 
     self = Date(timeIntervalSince1970: TimeInterval(milliseconds/1000)) 
    } 
} 

更新されるintに変換されていない、それは誰かに役立つことを願いますまた同じ問題があります

+2

そのエラーはなぜ発生しますか? 1970年以降の時間間隔は32ビットです。少なくとも私たちがこれに到達するまで:https://en.wikipedia.org/wiki/Year_2038_problem – DoesData

+1

@DoesDataもしあなたが私たちに1000を乗じているとわかったら。これはInt32の範囲外になります –