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!
Related
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.
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.
I have been working on a .NET/C# form that contains two time fields that are built out of dropdowns. Time A is made up of two dropdowns for Hours and Minutes and Time B is made up of two drop downs for hours and minutes too. I need to compare the two in order to ensure that Time A is always greater than B.
I could just use a CompareValidator to check the hours, that works BUT doesn't take into account the minutes. So lets assume the following scenarios:
A = 11:00 B = 12:15 is fine my validation accepts this as it should do
A = 11:15 B = 11:00 is accepted because the hours are equal but otherwise shouldn't pass as the minutes aren't validated
How would you ensure B is always equal to or greater than A where dropdowns are used? I would if I could change this but not permitted to do so.
I believe the best way is to have a method which encapsulates the comparison and internally is creating two DateTime values, a and b, and comparing them.
Such method will initialize a and b with the values of the UI.
Why not add a CustomValidator and write a little javascript function to compare the dates?
How do I get number of ticks per second of DateTime.UtcNow and convert it to a String value?
BAD QUESTION: try again Get ten millionths of a second
A particular value of DateTime doesn't have a "ticks per second" associated with it; ticks are ticks no matter which DateTime they're in. Ticks are 100 nanoseconds long, so there are 10,000,000 of them per second.
Now to get that as a string is as simple as the string literal "10000000"... although in general you would obtain a number and just call ToString() on it. For instance, you could use:
string ticksPerSecond = TimeSpan.TicksPerSecond.ToString();
Your question is a slightly odd one, so I wonder whether we're missing something... could you edit the question with more details about what you're trying to do. For example, are you trying to determine the number of ticks within the particular second of a particular DateTime? That's most easily done as:
long ticks = dt.Ticks % TimeSpan.TicksPerSecond;
You find the ticks per second as a constant on TimeSpan:
TimeSpan.TicksPerSecond
Not sure what you are trying to do though...
(DateTime.UtcNow.Ticks / TimeSpan.TicksPerSecond).ToString() // Total number of seconds...
I think you may want TimeSpan.TicksPerSecond.
Console.WriteLine("tps = {0}", TimeSpan.TicksPerSecond.ToString());
The number of ticks per second in a DateTime value is always 10000000. One tick is 100 nanoseconds.
So, if you want to convert that to a string:
10000000.ToString()
Greetings
I'm trying to do some DateTime math for various time zones and I wanted to take daylight savings into account. Lets say I have a TimeZoneInfo and i've determined the appropriate AdjustmentRule for a given DateTime. Lets also say the particular TimeZoneInfo i'm dealing with is specified as rule.DaylightTransitionStart.IsFixedDateRule == false, so I need to figure out if the given DateTime falls within the start/end TransitionTime.Week values.
This is where I'm getting confused, what is .NET considering as a "week"? My first thought was it probably used something like
DayOfWeek thisMarksWeekBoundaries = Thread.CurrentThread.CurrentUICulture.DateTimeFormat.FirstDayOfWeek;
and went through the calendar assigning days to week, incrementing week every time it crossed a boundary. But, if I do this for May 2010 there are 6 week boundary buckets, and the max valid value for TransitionTime.Week is 5 so this can't be right.
Whats the right way to slice up May 2010?
This article http://msdn.microsoft.com/en-us/library/system.timezoneinfo.transitiontime.isfixeddaterule.aspx shows how to extract the IsFixedDateRule == false, see DisplayTransitionInfo
I finally realized whats going on, I think the property name "Week" is what threw me off. There might be 6 weeks in May (depending on how you count them), but any particular DayOfWeek shows up at most 5 times. The Week property doesn't really refer to what week the DayOfWeek is showing up in, its the nth DayOfWeek for that month--with the magic value 5 meaning its last so either the max n is 4 or 5 for a given month.