Round Timespan Seconds [duplicate] - c#

This question already has answers here:
Can you round a .NET TimeSpan object?
(10 answers)
Closed 6 years ago.
Round timespan seconds I also need do same with .Hours and .Minutes
This:
1.53994 second
to:
1 second

This code worked for me.
TimeSpan ts = TimeSpan.FromSeconds(1.54);
Math.Round(ts.TotalSeconds);

Related

I'll find out the date of the next few days [duplicate]

This question already has answers here:
Datetime in C# add days
(7 answers)
Closed 3 years ago.
Hello friends I had a question
For example today
06/24/2019. What code should I use if I want to find the date of the next 20 days?
var d = DateTime.Now().AddDays(20);

Convert string in DateTime [duplicate]

This question already has an answer here:
DateTime losing Milliseconds
(1 answer)
Closed 5 years ago.
DateTime x = DateTime.ParseExact("2017_10_16 13:52:03.112","yyyy_MM_dd HH:mm:ss.fff", null);
Console.WriteLine(x);
"10/16/2017 1:52:03 PM"
It gives output as "10/16/2017 1:52:03 PM"
Here I am missing the milliseconds 112.
I have looked into many online posts but it does not work for me.
What you have is correct. Only Console.WriteLine(x); does not show you milliseconds by default.
If you ask for this: Console.WriteLine(x.ToString("fff")); you will see your milliseconds

Get Microseconds from DataTime.Now in C# [duplicate]

This question already has answers here:
C# time in microseconds
(4 answers)
How can I get the Windows system time with millisecond resolution?
(10 answers)
Closed 5 years ago.
Need to get the Timestamp value from system time with high resolution (microseconds).
I googled and found that we should use StopWatch. But stopwatch is used for difference between of two different time, is it correct?

How can I add 1 minute to current time that I get? webdriver c# [duplicate]

This question already has answers here:
Add hours or minutes to the current time
(4 answers)
Closed 7 years ago.
I get the current time and need to add 1 minute to it so It would be 1 minute ahead, how do I do that?
string date1 = System.DateTime.Now.ToString("HH:mm");
All you have to use is DateTime.AddMinutes
string date1 = System.DateTime.Now.AddMinutes(1).ToString("HH:mm");
Conversely, if you want to subtract a minute just use a negative parameter.
DateTime.AddMinutes method can do that.
If you want a string representation then do:
string date1 = System.DateTime.Now.AddMinutes(1).ToString("HH:mm");
As a side note, you can also supply a negative number to subtract minutes from current time like:
DateTime.Now.AddMinutes(-1)

Convert decimal (2.75) to time (2:45) [duplicate]

This question already has answers here:
C# String Format for hours and minutes from decimal
(3 answers)
Closed 9 years ago.
If I have a double like 2.75, is there a way in .Net to format it as '2:45'
If it is for example, 2.75555555555, it should round it to the nearest minute.
I would not mind coding this myself, but I am wondering if .Net can. I checked ToString but did not find anything.
Thanks
Use TimeSpan and its ToString formatter:
TimeSpan timespan = TimeSpan.FromHours(2.75);
string output = timespan.ToString("h\\:mm");
For example
TimeSpan.FromHours(2.75555).ToString("h\\:mm")
outputs
2:45

Categories