LoginSignup
5
2

More than 5 years have passed since last update.

unity > unix timeをSystem.DateTimeに変換する関数

Last updated at Posted at 2015-09-23
動作確認
Unity 5.1.3-f1 on MacOS X 10.8.5

"dt": 1442973600,
というunix timeをSystem.DateTimeに変換したい。

参考: SO

using UnityEngine;
using System.Collections;

using System; // for DateTime;

public class MainScript : MonoBehaviour {

    public static DateTime UnixTimeToDateTime(double unixTime){
        DateTime dt = 
            new DateTime (1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
        dt = dt.AddSeconds (unixTime);
        return dt;
    }

    void Start () {
        DateTime dt = UnixTimeToDateTime (1442973600);
        Debug.Log (dt.ToString ());
    }   
}

結果

09/23/2015 02:00:00
UnityEngine.Debug:Log(Object)
MainScript:Start() (at Assets/MainScript.cs:17)

AddSeconds()はミリ秒の処理に注意が必要と参考ページに記載があるが、日時取得なら問題ないと思われる。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2