Calculating time difference using textbox as value input - c#

I have a text box and the user enters 16.30 how would I take that value and compute it as 16hrs and 30mins in order to get the correct time difference

DateTime is for fully qualified dates and times. It sounds like what you want is TimeSpan.Parse, which can handle time differences in hours, minutes, seconds, etc.

Take a look at DateTime.Parse.

Related

Subtracting an unknown interval from a datetime

I am setting up a system to gather data from a database based on a user inputted start date and end date. The system will gather data averaged over an interval(1 hour, 6 hours, or one day for example). If the user does not input a start or end date I would like the program to set the start date to the current time minus the interval.
I currently have the user inputting the interval in the following format.
1m = 1 minute
1h = 1 hour
12h = 12 hours
3d = 3 days
So these values are not formatted like datetime. I could take the current datetime and subtract it by either minutes, hours, or days depending on the value appended (splitting on the number), but this would mean many if statements. What I would really like is a method to subtract a datetime by an arbitrary value Does anyone have a better solution?
Instead of providing predefined time intervals (that are implemented e. g. via a separate type/enum), it is much easier to let the user freely specify a TimeSpan.
This has two advantages:
The user is not restricted to predefined intervals
You can subtract the TimeSpan directly from your DateTime.Now
If restriction to limited intervals is a requirement, you can implement this in the view/window. But still this should be a TimeSpan.

Calculate length of time between two times in AM/PM

I have three DrowDownLists for selecting start time and another three for selecting end time. I need an asp.net program which will give duration between two time and after i entered start time then end time should be greater than start time. First DrowDownList contains 1 to 12 hours and second contains 0 to 59 minutes and third one is for selecting am/pm.
I don't know where to start in codebehind.
You could first turn your strings into TimeSpan, try out TimeSpan.Parse:
https://msdn.microsoft.com/en-us/library/se73z7b9(v=vs.110).aspx
With two TimeSpan objects, you will be able to calculate durations without having to worry on how to do it. To get the duration between two timespans, try out TimeSpan.Subtract:
https://msdn.microsoft.com/en-us/library/system.timespan.subtract(v=vs.110).aspx
hope this helps!

C# Display Milliseconds in hh:mm:ss format

I am trying to write a program that has the function to lock the computer after a specified amount of time once the program is activated. The problem im having is getting the time remaining to display properly. I am trying to do this using datetime vs a switch/if scenario. display a countdown timer based on a user specified about of time. More specifically what I want to do is
1) the user specifies amount of minutes
2) the minutes is programmatically converted to milliseconds
3 where im stuck) Milliseconds is converted and displayed via label in hh:mm:ss.
I have spent a couple days searching and I don't quite understand the MSDN examples and I haven't been able to come a cross the way to do this. Found plenty of examples for going from datetime to milliseconds though.
TimeSpan would be better suited since you're talking about a duration, not a point in time. You can create a TimeSpan from milliseconds and then format it with ToString():
int ms = 123456;
TimeSpan ts = TimeSpan.FromMilliseconds(ms);
Console.WriteLine(ts.ToString(#"hh\:mm\:ss"));

format TimeSpan summary in stimulsoft

I'm creating simple table report with column of TimeSpan type.
I'm summing its values, which leads into values bigger than 24 hours, into Text componnent.
{SumTime(DataBand1,Records.time)}
I'm trying to format Text field like HH:mm:ss, but for 25 hours it gives me 01:00:00 (or 1.01:00:00 with general formatting) instead of 25:00:00 what is my goal.
Edit: The problem is not how to format timespan, but how to associate formatted value into Stimulsoft's Text component.
The Hours property has a maximum of 24h. You could format it yourself using String.Format and TimeSpan.TotalHours:
string text = string.Format("{0}:{1}:{2}",
(int) Records.time.TotalHours, // TotalHours is double
Records.time.Minutes,
Records.time.Seconds);
I know this is kind of an old question, but I stumbled upon it looking for a solution myself. Having done some digging, here's what I've come up with:
Assuming you already have a timespan variable (either setting it in the datasource, or having a variable set with DateDiff), you can format it with the following:
{string.Format("{0}:{1}:{2}",
(int) Variable1.TotalHours,
Variable1.Minutes,
Variable1.Seconds)}
Lets say you have two different fields (named Date1 and Date2) that you need to get the difference of, and don't feel like putting it into a variable:
{string.Format("{0}:{1}:{2}",
(int) DateDiff(Date2,Date1).TotalHours,
DateDiff(Date2,Date1).Minutes,
DateDiff(Date2,Date1).Seconds)}

What is a meaningfull datatype to save hours

What is a good data-type for saving hours in .net?
Is it better to use the decimal type or is the double data-type more appropriate. With hours I mean values such as:
2 for two hours
1.5 for 90 minutes
8.25 for 8 hours and 15 minutes.
A good way to represent a number of hours is to use a TimeSpan:
TimeSpan hours = TimeSpan.FromHours(2);
Given the choice between decimal or double I'd probably go for double as there is typically no expectation that the amount of time is represented exactly. If you need an exact decimal representation of your fractional number of hours (which seems unlikely) then use decimal.
You could also consider storing it as an integer in for example seconds, milliseconds or ticks.
The best datatype to store hours is the one designed for it - TimeSpan.
It has methods that allow you to add/subtract/convert it.
As for storage in a database, it really depends on what you are using this for and what kind of resolution is required.
I would use the time datatype - as it will hold the range:
00:00:00.0000000 through 23:59:59.9999999
However, if you need to hold more than 24 hours in this field, you may want to consider a tinyint or int holding the number of minutes (assuming that is the maximum time resolution you require).
In SQL Server use INT or DECIMAL. TIME isn't really ideal for storing a duration because TIME defines a point in time within the 24 hour clock whereas duration is simply an integer or decimal value. You cannot do addition or subtraction with TIME values and there is no obvious way to use TIME to store durations greater than 24hrs.
Why don't use TIME?
You can use DATEADD with TIME to manipulate it easier:
SELECT DATEADD(minute, 30, CAST('2:00:00' AS TIME))
becomes 02:30:00.0000000. And so on..

Categories

Resources