「C#でUnixTimeを扱う」の版間の差分
imported>Administrator ページの作成:「<syntaxhighlight lang="csharp"> public static class DateTimeExtensions { private static readonly DateTime UNIX_EPOCH = new DateTime(1970, 1, 1, 0, 0, 0,...」 |
Administrator (トーク | 投稿記録) 編集の要約なし |
||
| (他の1人の利用者による、間の2版が非表示) | |||
| 1行目: | 1行目: | ||
DateTimeの拡張メソッドを用意しておくと便利。 | |||
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
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; | |||
} | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
[[カテゴリ:.NET]] | |||