「C#でUnixTimeを扱う」の版間の差分
imported>Administrator 編集の要約なし |
Administrator (トーク | 投稿記録) 編集の要約なし |
||
| 1行目: | 1行目: | ||
DateTimeの拡張メソッドを用意しておくと便利。 | 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]] | [[カテゴリ:.NET]] | ||
2024年8月19日 (月) 07:17時点における最新版
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;
}
}