how to Convert DateTime Now To second - c#

I have the input 15:20:30
I want to convert to seconds.

Seeing as though you haven't specified the question properly I have interpreted it to represent 15 hours 20 minutes and 30 seconds, as opposed to DateTime.Now. (Obviously this is the same as "How many seconds since midnight")
TimeSpan MySpan = new TimeSpan(15, 20, 30);
MySpan.TotalSeconds;
Although if you're only wanting the Seconds from the current DateTime.Now (this is not the TotalSeconds, just the current minutes seconds), just use:
DateTime.Now.Second

var dt = DateTime.Now;
var ticks = dt.Ticks;
var seconds = ticks/TimeSpan.TicksPerSecond;
Each tick is 100 nanoseconds.

Not sure what you really want, but if you want to compute the number of seconds from 15 hours, 20 minutes and 30 seconds you can do this:
Int32 GetSeconds(Int32 hours, Int32 minutes, Int32 seconds) {
return ((hours*60) + minutes)*60 + seconds;
}

Related

Formatting a TimeSpan object is always displaying 00 for milliseconds

C# WinForms here.
I need to extract Seconds and Milliseconds from a similar string: "13.9" where 13 are Seconds and 9 Milliseconds.
To do this i use a String.Split() function and after i create a TimeSpan object with the corresponding values (suppose TimeString is "13.9"):
private TimeSpan TimeSplit(string TimeString)
{
var Seconds = Int32.Parse(TimeString.Split('.')[0]); //output 13
var Milliseconds = Int32.Parse(TimeString.Split('.')[1]); //output 9
var ts = new TimeSpan(0, 0, 0, Milliseconds, Decimals);
return ts;
}
Now i need to use the TimeSpan to show formatted output:
TimeSpan TempTs = TimeSplit(output);
SetTextMP(TempTs.ToString(#"hh\:mm\:ss\.ff"));
I need to have an output like: hh:mm:ss.ff but in my try Milliseconds(ff) stay fixed to 0. I checked and they are there..
As stated in the comments, the issue here is because 9 milliseconds amounts to 0.009 seconds. Running it with format specifier fff displays the complete millisecond value.

Calculate total number of Hours asp.net c#

I have done the following piece of code to add the Hours and thus calculate total number of hours.
string hour1="48.40";
string hour2 = "45.35";
TimeSpan ts = new TimeSpan(int.Parse(hour1.Split('.')[0]),int.Parse(hour1.Split('.')[1]),
0);
TimeSpan ts1 = new TimeSpan(int.Parse(hour2.Split('.')[0]),int.Parse(hour2.Split('.')[1]),
0);
Double total = (ts.TotalHours) + (ts1.TotalHours);
The problem here is when i add hour1 and hour2 the total comes as 64.25 which actually should have been 64.15
This is just one of the test case, if i put hour1= 40.00 and hour2= 40.10 than the value in the total comes as 80.166666666666657 which actually should have been 80.10
can anyone help me understand what am i doing wrong and what is the correct way to add HOUR and get total number of hours ?
Actually you're getting correct result - just mixing minutes and fractional parts of hours.
80 hrs 10 mins is 80 1/6 hours
64 hours 15 mins is 64 1/4 hours
It gets a little strange when you have timestamps put into strings. But if you need to do it like this, this code should work
string hour1="48.40";
string hour2 = "45.35";
//find total number of minutes for each hour above
int minutes1 = int.Parse(hour1.Split('.')[0])*60+int.Parse(hour1.Split('.')[1]);
int minutes2 = int.Parse(hour2.Split('.')[0])*60+int.Parse(hour2.Split('.')[1]);
//calculate back to hours and minutes and reassemble as a string
string result = (minutes1+minutes2)/60+"."+(minutes1+minutes2)%60;
And I hope you are expecting 94.15 and not 64.15 in your example above.
You may use next code to get result you would like to:
string hour1 = "48.40";
string hour2 = "45.35";
TimeSpan ts = new TimeSpan(int.Parse(hour1.Split('.')[0]), int.Parse(hour1.Split('.')[1]),
0);
TimeSpan ts1 = new TimeSpan(int.Parse(hour2.Split('.')[0]), int.Parse(hour2.Split('.')[1]),
0);
TimeSpan total = ts + ts1;
int hours = (int)total.TotalHours;
int minutes = total.Minutes;

Problem finding difference between two time intervals

How can I find difference between two time intervals.
Like 13:45:26.836 - 14:24:18.473 which is of the format "Hour:Min:Sec:Millisecs". Now i need to find the time difference between these two times.
How can i do this in C#.?
Thanks in advance.
Basically, what you need to do is put those time values into DateTime structures. Once you have your two DateTime variables, just subtract them from one another - the result is a variable of type TimeSpan:
DateTime dt1 = new DateTime(2010, 5, 7, 13, 45, 26, 836);
DateTime dt2 = new DateTime(2010, 5, 7, 14, 24, 18, 473);
TimeSpan result = dt2 - dt1;
string result2 = result.ToString();
TimeSpan has a ton of properties that get sets - the difference in all sorts of units, e.g. milliseconds, seconds, minutes etc. You can also just do a .ToString() on it to get a string representation of the result. In result2, you'll get something like this:
00:38:51.6370000
Is that what you're looking for?
i'm posting an example;
you can check it and adapt your program,
/* Read the initial time. */
DateTime startTime = DateTime.Now;
Console.WriteLine(startTime);
/* Do something that takes up some time. For example sleep for 1.7 seconds. */
Thread.Sleep(1700);
/* Read the end time. */
DateTime stopTime = DateTime.Now;
Console.WriteLine(stopTime);
/* Compute the duration between the initial and the end time.
* Print out the number of elapsed hours, minutes, seconds and milliseconds. */
TimeSpan duration = stopTime - startTime;
Console.WriteLine("hours:" + duration.Hours);
Console.WriteLine("minutes:" + duration.Minutes);
Console.WriteLine("seconds:" + duration.Seconds);
Console.WriteLine("milliseconds:" + duration.Milliseconds);
Find the number of seconds; subtract both numbers and then you can figure out the time difference. Depending on the programming language you use, I am positive their must be a library that can handle it.
//Start off with a string
string time1s = "13:45:26.836";
string time2s = "14:24:18.473";
TimeSpan interval = DateTime.Parse(time2s) - DateTime.Parse(time1s);
This will produce a result of:
Days 0 int Hours 0 int
Milliseconds 637 int
Minutes 38 int Seconds 51 int
Ticks 23316370000 long
TotalDays 0.02698653935185185 double
TotalHours 0.64767694444444446 double
TotalMilliseconds 2331637.0 double
TotalMinutes 38.860616666666665 double
TotalSeconds 2331.6369999999997 double

Work out min and hours

I have a minute value and i want to have to 2 string values one with how many hours and the other with the minutes, e.g.:
Value - 121 minutes
string hours = 2
string minutes = 1
Value - 58 minutes
string hours = 0
string minutes = 58
How can I work this out in C#?
var span = System.TimeSpan.FromMinutes(121);
var hours = ((int)span.TotalHours).ToString();
var minutes = span.Minutes.ToString();
The ToString() is because you asked for string values ...
TotalHours are the complete hours in the TimeSpan, they can be more than 24 (whereas the "Hours" field has a maximum of 24)
Oh, and on second thought: Why use the TimeSpan and not calculate it yourself? Because TimeSpan is already there debugged & tested by Microsoft, it has a nice clean interface (looking at the code you easily see whats going on without having to follow a calculation mentally) and it easily extends to further solutions. (Have the input in seconds? Use TimeSpan.FromSeconds(). Want the days? Use span.TotalDays ...)
Update:
I just noticed mistake in my answer: TotalHours returns a fractional value of all the hours, so we have to truncate it to an integer before converting it to a string.
Use a Timespan struct and its Parse method.
int value = 121;
int hours = value / 60; // 2
int minutes = value % 60; // 1
string strHours = hours.ToString();
string strMinutes = minutes.ToString();
int value = 121;
int hours = value / 60;
int minutes = value % 60;
int value = 121;
TimeSpan timeSpan = TimeSpan.FromMinutes(value);
// gives you the rounded down value of 2
int hours = timeSpan.Hours;
// gives you the minutes left of the hour
int minutes = value - (hours * 60);

How do I convert ticks to minutes?

I have a ticks value of 28000000000 which should be 480 minutes but how can I be sure? How do I convert a ticks value to minutes?
TimeSpan.FromTicks(28000000000).TotalMinutes;
A single tick represents one hundred nanoseconds or one ten-millionth of a second. FROM MSDN.
So 28 000 000 000 * 1/10 000 000 = 2 800 sec.
2 800 sec /60 = 46.6666min
Or you can do it programmaticly with TimeSpan:
static void Main()
{
TimeSpan ts = TimeSpan.FromTicks(28000000000);
double minutesFromTs = ts.TotalMinutes;
Console.WriteLine(minutesFromTs);
Console.Read();
}
Both give me 46 min and not 480 min...
You can do this way:
TimeSpan duration = new TimeSpan(tickCount)
double minutes = duration.TotalMinutes;
The clearest way in my view is to use TimeSpan.FromTicks and then convert that to minutes:
TimeSpan ts = TimeSpan.FromTicks(ticks);
double minutes = ts.TotalMinutes;
there are 600 million ticks per minute.
ticksperminute
TimeSpan.FromTicks( 28000000000 ).TotalMinutes;
DateTime mydate = new Date(2012,3,2,5,2,0);
int minute = mydate/600000000;
will return minutes of from given date (mydate) to current time.hope this help.cheers

Categories

Resources