「C#でUnixTimeを扱う」の版間の差分

提供: MonoBook
ナビゲーションに移動 検索に移動
imported>Administrator
(ページの作成:「<syntaxhighlight lang="csharp"> public static class DateTimeExtensions { private static readonly DateTime UNIX_EPOCH = new DateTime(1970, 1, 1, 0, 0, 0,...」)
 
imported>Administrator
 
(同じ利用者による、間の1版が非表示)
1行目: 1行目:
 +
DateTimeの拡張メソッドを用意しておくと便利。
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
     public static class DateTimeExtensions  
 
     public static class DateTimeExtensions  
11行目: 12行目:
 
     }
 
     }
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
[[カテゴリ:.NET]]

2018年8月14日 (火) 04:36時点における最新版

DateTimeの拡張メソッドを用意しておくと便利。

    public static class DateTimeExtensions 
    {
        private static readonly DateTime UNIX_EPOCH = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        public static long ToUnixTime(this DateTime dateTime)
        {
            double nowTicks = (dateTime.ToUniversalTime() - UNIX_EPOCH).TotalSeconds;
            return (long)nowTicks;
        }        
    }