Jaki jest najlepszy sposób na konwersję sekund na czas (godzina:minuty:sekundy:milisekundy)?
Jaki jest najlepszy sposób na konwersję sekund na (godzina:minuty:sekundy:milisekundy)?
Powiedzmy, że mam 80 sekund, czy są jakieś specjalistyczne klasy/techniki w. Net, które pozwoliłyby mi przekonwertować te 80 sekund na (00h:00m:00s:00ms) format jak DateTime czy coś?
12 answers
Dla . Net Użyj klasy TimeSpan.
TimeSpan t = TimeSpan.FromSeconds( secs );
string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms",
t.Hours,
t.Minutes,
t.Seconds,
t.Milliseconds);
(Jak zauważył Inder Kumar Rathore) dla . NET > 4.0 możesz użyć
TimeSpan time = TimeSpan.FromSeconds(seconds);
//here backslash is must to tell that colon is
//not the part of format, it just a character that we want in output
string str = time .ToString(@"hh\:mm\:ss\:fff");
(od Nicka Molyneux) upewnij się, że sekundy są mniejsze niż TimeSpan.MaxValue.TotalSeconds
, aby uniknąć wyjątku.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-09 20:09:09
Dla . NET > 4.0 możesz użyć
TimeSpan time = TimeSpan.FromSeconds(seconds);
//here backslash is must to tell that colon is
//not the part of format, it just a character that we want in output
string str = time .ToString(@"hh\:mm\:ss\:fff");
Lub jeśli chcesz format daty, Możesz również to zrobić
TimeSpan time = TimeSpan.FromSeconds(seconds);
DateTime dateTime = DateTime.Today.Add(time);
string displayTime = date.ToString("hh:mm:tt");
Więcej można sprawdzić niestandardowe ciągi formatu TimeSpan
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-05 08:56:54
Jeśli wiesz, że masz kilka sekund, możesz utworzyć wartość TimeSpan, wywołując TimeSpan.FromSeconds:
TimeSpan ts = TimeSpan.FromSeconds(80);
Można następnie uzyskać liczbę dni, godzin, minut lub sekund. Lub użyj jednego z przeciążeń ToString, aby wydrukować go w dowolny sposób.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-21 00:20:06
TimeSpan.FromSeconds(80);
Http://msdn.microsoft.com/en-us/library/system.timespan.fromseconds.aspx
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-21 00:21:18
Konstruktor TimeSpan pozwala przejść w sekundach. Wystarczy zadeklarować zmienną typu TimeSpan ilość sekund. Ex:
TimeSpan span = new TimeSpan(0, 0, 500);
span.ToString();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-21 00:24:07
Zrobiłem kilka benchmarków, aby zobaczyć, co jest najszybszym sposobem i to są moje wyniki i wnioski. Uruchomiłem każdą metodę 10M razy i dodałem komentarz ze średnim czasem na bieg.
Jeśli Twoje milisekundy wejściowe nie są ograniczone do jednego dnia (twój wynik może wynosić 143: 59: 59.999), są to Opcje, od szybszych do wolniejszych:
// 0.86 ms
static string Method1(int millisecs)
{
int hours = millisecs / 3600000;
int mins = (millisecs % 3600000) / 60000;
// Make sure you use the appropriate decimal separator
return string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", hours, mins, millisecs % 60000 / 1000, millisecs % 1000);
}
// 0.89 ms
static string Method2(int millisecs)
{
double s = millisecs % 60000 / 1000.0;
millisecs /= 60000;
int mins = millisecs % 60;
int hours = millisecs / 60;
return string.Format("{0:D2}:{1:D2}:{2:00.000}", hours, mins, s);
}
// 0.95 ms
static string Method3(int millisecs)
{
TimeSpan t = TimeSpan.FromMilliseconds(millisecs);
// Make sure you use the appropriate decimal separator
return string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}",
(int)t.TotalHours,
t.Minutes,
t.Seconds,
t.Milliseconds);
}
Jeśli Twoje milisekundy wejściowe są ograniczone do jednego dnia (Twój wynik nigdy nie będzie większy niż 23: 59: 59.999), są to opcje, od szybszego do wolniejszego:
// 0.58 ms
static string Method5(int millisecs)
{
// Fastest way to create a DateTime at midnight
// Make sure you use the appropriate decimal separator
return DateTime.FromBinary(599266080000000000).AddMilliseconds(millisecs).ToString("HH:mm:ss.fff");
}
// 0.59 ms
static string Method4(int millisecs)
{
// Make sure you use the appropriate decimal separator
return TimeSpan.FromMilliseconds(millisecs).ToString(@"hh\:mm\:ss\.fff");
}
// 0.93 ms
static string Method6(int millisecs)
{
TimeSpan t = TimeSpan.FromMilliseconds(millisecs);
// Make sure you use the appropriate decimal separator
return string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}",
t.Hours,
t.Minutes,
t.Seconds,
t.Milliseconds);
}
W przypadku, gdy dane wejściowe wynoszą tylko sekundy, metody są nieco szybsze. Ponownie, jeśli sekundy wejściowe nie są ograniczone do jednego dnia (wynik może być 143:59:59):
// 0.63 ms
static string Method1(int secs)
{
int hours = secs / 3600;
int mins = (secs % 3600) / 60;
secs = secs % 60;
return string.Format("{0:D2}:{1:D2}:{2:D2}", hours, mins, secs);
}
// 0.64 ms
static string Method2(int secs)
{
int s = secs % 60;
secs /= 60;
int mins = secs % 60;
int hours = secs / 60;
return string.Format("{0:D2}:{1:D2}:{2:D2}", hours, mins, s);
}
// 0.70 ms
static string Method3(int secs)
{
TimeSpan t = TimeSpan.FromSeconds(secs);
return string.Format("{0:D2}:{1:D2}:{2:D2}",
(int)t.TotalHours,
t.Minutes,
t.Seconds);
}
I jeśli Twoje sekundy wejściowe są ograniczone do jednego dnia (Twój wynik nigdy nie będzie większy niż 23:59:59):
// 0.33 ms
static string Method5(int secs)
{
// Fastest way to create a DateTime at midnight
return DateTime.FromBinary(599266080000000000).AddSeconds(secs).ToString("HH:mm:ss");
}
// 0.34 ms
static string Method4(int secs)
{
return TimeSpan.FromSeconds(secs).ToString(@"hh\:mm\:ss");
}
// 0.70 ms
static string Method6(int secs)
{
TimeSpan t = TimeSpan.FromSeconds(secs);
return string.Format("{0:D2}:{1:D2}:{2:D2}",
t.Hours,
t.Minutes,
t.Seconds);
}
Jako ostatni komentarz dodam, że zauważyłem, że string.Format
jest nieco szybsze, jeśli używasz D2
zamiast 00
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-23 05:31:56
Sugerowałbym użycie klasy TimeSpan
do tego.
public static void Main(string[] args)
{
TimeSpan t = TimeSpan.FromSeconds(80);
Console.WriteLine(t.ToString());
t = TimeSpan.FromSeconds(868693412);
Console.WriteLine(t.ToString());
}
Wyjścia:
00:01:20
10054.07:43:32
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-21 00:24:38
W VB.NET, ale w C#to samo:
Dim x As New TimeSpan(0, 0, 80)
debug.print(x.ToString())
' Will print 00:01:20
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-13 01:52:41
Dlaczego ludzie potrzebują TimeSpan i DateTime, jeśli mamy DateTime.AddSeconds()?
var dt = new DateTime(2015, 1, 1).AddSeconds(totalSeconds);
Data jest dowolna. suma sekund może być większa niż 59 i jest Podwójna. Następnie możesz sformatować swój czas, jak chcesz, używając DateTime.ToString (): {]}
dt.ToString("H:mm:ss");
To nie działa, jeśli suma sekund 59:
new DateTime(2015, 1, 1, 0, 0, totalSeconds)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-14 21:26:22
Dla . NET (e. x: Unity) możesz napisać metodę rozszerzenia, aby TimeSpan.ToString(string format)
zachowywała się jak . NET > 4.0
public static class TimeSpanExtensions
{
public static string ToString(this TimeSpan time, string format)
{
DateTime dateTime = DateTime.Today.Add(time);
return dateTime.ToString(format);
}
}
I z dowolnego miejsca w kodzie możesz go używać w następujący sposób:
var time = TimeSpan.FromSeconds(timeElapsed);
string formattedDate = time.ToString("hh:mm:ss:fff");
W ten sposób możesz sformatować dowolny obiekt TimeSpan
, po prostu wywołując ToString z dowolnego miejsca Twojego kodu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-24 19:10:22
private string ConvertTime(double miliSeconds)
{
var timeSpan = TimeSpan.FromMilliseconds(totalMiliSeconds);
// Converts the total miliseconds to the human readable time format
return timeSpan.ToString(@"hh\:mm\:ss\:fff");
}
//Test
[TestCase(1002, "00:00:01:002")]
[TestCase(700011, "00:11:40:011")]
[TestCase(113879834, "07:37:59:834")]
public void ConvertTime_ResturnsCorrectString(double totalMiliSeconds, string expectedMessage)
{
// Arrange
var obj = new Class();;
// Act
var resultMessage = obj.ConvertTime(totalMiliSeconds);
// Assert
Assert.AreEqual(expectedMessage, resultMessage);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-31 03:30:47
Aby uzyskać całkowitą liczbę sekund
var i = TimeSpan.FromTicks(startDate.Ticks).TotalSeconds;
I aby uzyskać datetime z sekund
var thatDateTime = new DateTime().AddSeconds(i)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-03 07:29:22