I have seen posts on how to define a specific time but couldn't find on discrepancy issues. Hope someone can enlighten me.
I want to define a schedule time as let's say 11.10 a.m in windows service. I have my code like this.
scheduledDailyRun = DateTime.Today.AddHours(11.10); // testing
eventLog1.WriteEntry("Scheduled defined # " + scheduledDailyRun.ToString()); //<<--DEBUG
When I check eventlog1, it says "Scheduled defined # 26/11/2013 11:06:00 AM". Why is it not 11.10 which is defined up there as DateTime.Today.AddHours(11.10)?
.10 = 10%
10% of 1 hour (60 minutes) = 6 minutes.
11.06 is correct.
Use .AddHours(11).AddMinutes(10); instead
To add 11 hrs 10 min, use following
DateTime.Today.AddHours(11).AddMinutes(10);
Related
I am trying to build an Alexa skill that asks the user for an arrival time and I am expecting the answer to be in 24 hour format. So the user would say eighteen o five for 18:05.
I am struggling to find a way for Alexa to understand this without forcing the user down the 12 hour route.
Any suggestions welcome.
The AMAZON.TIME slot type is available but, unfortunately, doesn't work like you want.
If you're happy for users to say "six fifteen pm" it will pass a 24H time to your skill.
This might be unconventional, but have you tried
AMAZON.FOUR_DIGIT_NUMBER
If you think about it 24 hour format is also a 4 digit number
I got around this in the end by using just the one custom slot type which takes the whole time in a malformed state. 18:05 comes in as 18o5. I then use a method within my code to tidy that up. Not the most scientific way to do this, but seems to work.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to write a program that calculates the addition of two times in C# Example: time1 = 04:21:57 time2= 01:54:26 result = 06:16:23 . I'm still not very familiar with the language and I'm stuck like glue on this problem unable to find a solution. I would appreciate your help.
Use TimeSpan.Add Method (TimeSpan)
TimeSpan time1 = TimeSpan.Parse("04:21:57");
TimeSpan time2 = TimeSpan.Parse("01:54:26");
TimeSpan result = time1.Add(time2);
Console.WriteLine(result);
It appears that what you call "time" is actually a time span, i.e. the difference between two absolute times.
If this is correct, .NET offers a good data type to deal with the problem: it's called TimeSpan. You can perform parsing and addition like this:
var a = TimeSpan.Parse("01:54:26");
var b = TimeSpan.Parse("06:16:23");
var c = a + b;
Console.WriteLine(c);
Demo.
Note that the result may exceed 24 hours, in which case the sum would be expressed in days, hours, minutes, and seconds.
I will not give you the code, I'll just give you the idea:
Since you haven't provided enough information, I'm gonna assume that time1 and time2 are strings. You need to split them with a delimeter ":", then you first need to add seconds, and you'll calculate the module of the given sum by 60 (e.g. if you get 72, module is 12, so that'll be your result) and also division of that sum with 60 (you'll get either 1 or 0, do integer division).
You will add this to the sum of minutes as the transfer. Do the same thing for the sum of minutes, module by 60, that's the minutes, divide by 60 and add to the sum of hours.
First, try to define a class "Time", who have some data int value (hours, minutes, sec) and define some basics private method (like increase hours when you have 60 min, and set to 0 min, same fonction for secunds)
Now, in your program main, define two time objectif, and with the method you've developed, you'll have an output.
I'm looking for a way to format time duration originally expressed in hours (as a 'double' variable) for an ASP.NET web app written in C#. I need a short version that has only 2 significant values. For instance:
1h:20m
2d:20h
2mo:12d
5y:2mo
I searched and it seems like C# does not have a built-in function for what I need.
So I decided to write my own but I'm stumped with correct formatting of all the parts. For instance, I may get a string, such as "1d:24h", or for a simple 2 months, I may get "1mo:29d"
PS. The problem I've encountered is in defining how many days are in a month and in a year.
DateTime's are renowned for being an annoying task.
As there is no indication as to which months you are referring to, with the given information this would be impossible. Months can have 28, 29, 30 and 31 days depending on what month/year you are taking into consideration..
Without an indication as to which months you are dealing with, the flip from 1 to 2 months would be a random guess as to which day you make the transition. You will either have to add in more incoming parameters to account for this, or explain to the user that a month is considered x days and only x days.
Another thing to consider would be daylight savings. 1pm + 24 hours may not be 1pm the next day. In such circumstances with you or the end user may wish to consider such days 23h=1d or 25h=1d.
How I have to convert Task.Duration that get valid result?
I find explanations about this property in MSDN:
Gets or sets the duration (in minutes) of a task.
But it doesn't work correct.
If I divided result by 60 (minutes in hour) and 24(hours in day) I get incorrect result.
But if I divided by 20 and 24 all it's ok. And I don't understand why.
I use C# on .Net 3.5 and Office Primary Interop Assemblies ( Microsoft.Office.Interop.MSProject for office 2010).
I use that code :
void SetProperties(MSProject.Task o, string version)
{
Wbs = o.WBS.ToString();
Name = o.Name.ToString();
StartDate = (System.DateTime) o.Start;
FinishDate = (System.DateTime)o.Finish;
Iteration = version;
duration = (Convert.ToInt16(o.Duration)/10/24).ToString();//after result //divided by 2 I get correct result. Why?
}
thanks
The reason that it doesn't work like you expect is because in a day you do not have 24 hours of working time. The Duration of a task is the amount of working time between the start and finish, not the absolute number of hours.
Since the default number of working hours in a day is 8, you divide the total minutes by 480 (60 min * 8 hours) to get the number of days. Your calculation of 20 * 24 just so happens to also equal 480, so you stumbled upon the correct number.
Of course, do not expect that Start + Duration (in days) is going to equal your Finish date. That's because you also have to factor in non-working days, like weekends. So you can have a 3 day task that starts on Friday, and it will not finish until the end of the day on Tuesday (5 calendar days).
Delphi:
SecondsBetween(StrToDateTime('16/02/2009 11:25:34 p.m.'), StrToDateTime('1/01/2005 12:00:00 a.m.'));
130289133
C#:
TimeSpan span = DateTime.Parse("16/02/2009 11:25:34 p.m.").Subtract(DateTime.Parse("1/01/2005 12:00:00 a.m."));
130289134
It's not consistent either. Some dates will add up the same, ie..
TimeSpan span = DateTime.Parse("16/11/2011 11:25:43 p.m.").Subtract(DateTime.Parse("1/01/2005 12:00:00 a.m."));
SecondsBetween(StrToDateTime('16/11/2011 11:25:43 p.m.'), StrToDateTime('1/01/2005 12:00:00 a.m.'));
both give
216905143
The total amount of seconds is actually being used to encode data, and I'm trying to port the application to C#, so even one second completely throws everything off.
Can anybody explain the disparity? And is there a way to get c# to match delphi?
Edit: In response to suggestions that it might be leap second related: Both date ranges contain the same amount of leap seconds (2), so you would expect a mismatch for both. But instead we're seeing inconsistency
16/02/2009 - 1/01/2005 = Delphi and C# calculate a different total seconds
16/11/2011 - 1/01/2005 = They calculate the same total seconds
The issue it seems related to this QC 59310, the bug was fixed in Delphi XE.
One will likely deal with Leap Seconds. However, .NET does not as far as I'm aware.
You don't mention how you convert the c# TimeSpan into a number. The TotalSeconds property is a floating point value - perhaps it's a rounding problem in the double to int conversion?