我正在寻找在 C# 中将 DateTime 转换为 Swatch 互联网时间。我什么也没找到。如何将 DateTime 转换为节拍?
编辑:找到解决方案,将尽快将其发布为答案:
var time = DateTime.Now; //get the current time
var utc1 = time.ToUniversalTime().AddHours(1); //convert the given DateTime to universal time and add one hour (internet time is based on UTC+1)
var beats = utc1.TimeOfDay.TotalMilliseconds / 86400d; //get the milliseconds of the given day and divide it to the number of seconds of a day
在一行中:
var beats = DateTime.Now.ToUniversalTime().AddHours(1).TimeOfDay.TotalMilliseconds / 86400d
作为日期时间扩展:
public static class DateTimeExtensions
{
public static double ToInternetTimeDouble(this DateTime time)
{
return time.ToUniversalTime().AddHours(1).TimeOfDay.TotalMilliseconds / 86400d;
}
public static int ToInternetTimeInt(this DateTime time)
{
return Convert.ToInt32(Math.Floor(time.ToInternetTimeDouble()));
}
public static string ToInternetTimeStr(this DateTime time)
{
return time.ToInternetTimeStr(false);
}
public static string ToInternetTimeStr(this DateTime time, bool decimals)
{
return string.Format(CultureInfo.InvariantCulture, "@{0}", decimals ? string.Format(CultureInfo.InvariantCulture, "{0:0.00}", time.ToInternetTimeDouble()) : time.ToInternetTimeInt().ToString(CultureInfo.InvariantCulture));
}
}