从 DateTime c# 中删除秒数
Remove Seconds from DateTime c#
我有一段代码将 unix 时间戳值转换为标准时间,但它是 returning 小时、分钟和秒。我希望它只有 return 小时和分钟。
convertSunriseTime = UnixTimeStampToDateTime(currentSunriseTime).ToString();
convertSunsetTime = UnixTimeStampToDateTime(currentSunsetTime).ToString();
private static TimeSpan UnixTimeStampToDateTime(long unixTimeStamp)
{
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
return dtDateTime.TimeOfDay;
}
然后变量 convertSunriseTime
和 convertSunsetTime
以秒返回。
这可以完成工作:
private static TimeSpan UnixTimeStampToDateTime(long unixTimeStamp)
{
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
dtDateTime = DateTime.FromMinutes((int)dtdateTime.TotalMinutes);
return dtDateTime.TimeOfDay;
}
所以你想格式化你的值以显示小时和分钟。你可以这样做:
convertSunriseTime = UnixTimeStampToDateTime(currentSunriseTime).ToString("hh\:mm");
这里,ToString() 方法的参数指定您只对将小时和分钟部分放入结果 string
感兴趣。这叫做 custom TimeSpan format string. There is also a set of standard ones: Standard TimeSpan format strings
Can you try this way
convertSunriseTime = UnixTimeStampToDateTime(currentSunriseTime).ToString();
convertSunsetTime = UnixTimeStampToDateTime(currentSunsetTime).ToString();
<pre>private static TimeSpan UnixTimeStampToDateTime(long unixTimeStamp) </pre>
<pre>{</pre>
<pre>DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);</pre>
<pre>dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);</pre>
<pre>return dtDateTime.TimeOfDay;</pre>
<pre>}</pre>
我有一段代码将 unix 时间戳值转换为标准时间,但它是 returning 小时、分钟和秒。我希望它只有 return 小时和分钟。
convertSunriseTime = UnixTimeStampToDateTime(currentSunriseTime).ToString();
convertSunsetTime = UnixTimeStampToDateTime(currentSunsetTime).ToString();
private static TimeSpan UnixTimeStampToDateTime(long unixTimeStamp)
{
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
return dtDateTime.TimeOfDay;
}
然后变量 convertSunriseTime
和 convertSunsetTime
以秒返回。
这可以完成工作:
private static TimeSpan UnixTimeStampToDateTime(long unixTimeStamp)
{
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
dtDateTime = DateTime.FromMinutes((int)dtdateTime.TotalMinutes);
return dtDateTime.TimeOfDay;
}
所以你想格式化你的值以显示小时和分钟。你可以这样做:
convertSunriseTime = UnixTimeStampToDateTime(currentSunriseTime).ToString("hh\:mm");
这里,ToString() 方法的参数指定您只对将小时和分钟部分放入结果 string
感兴趣。这叫做 custom TimeSpan format string. There is also a set of standard ones: Standard TimeSpan format strings
Can you try this way
convertSunriseTime = UnixTimeStampToDateTime(currentSunriseTime).ToString();
convertSunsetTime = UnixTimeStampToDateTime(currentSunsetTime).ToString();
<pre>private static TimeSpan UnixTimeStampToDateTime(long unixTimeStamp) </pre>
<pre>{</pre>
<pre>DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);</pre>
<pre>dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);</pre>
<pre>return dtDateTime.TimeOfDay;</pre>
<pre>}</pre>