How to check if a timespan in negative. C# - c#

The title says it all. I'm subtracting two DateTimes and I want to see if the returned Timespan is negative. How do I do this?
// dt1 and dt2 are DateTimes.
TimeSpan ts = dt1.Subtract(dt2);
ts.isNegative() // or something like that

TimeSpan has a Compare method on it, or you can do < TimeSpan.Zero or you could have just compared the two DateTimes in the first place and skipped creating the TimeSpan entirely.

Related

Convert Date to Milliseconds

I am working with Visual Studio 2010, MVC 3 and C#. I am creating some highcharts and need to have the x-axis be a date. I am pulling the dates from a database and adding them to and array that will then be passed to highcharts. I think highcharts requires the dates to be in millisecond format. Ho do I go about converting a DateTime of '12/20/2011 5:10:13 PM" for example to milliseconds?
Once you figure out what you want to calculate milliseconds from, you can just take one DateTime object from another to get a TimeSpan object. From TimeSpan you can get TotalMilliseconds.
In other words, if start and end are DateTime objects, you can do this:
double milliseconds = (end - start).TotalMilliseconds;
You can use the DateTime.Ticks property and convert the value to milliseconds.
The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.
A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.
The .Ticks in C# DateTime gives you the value of any time in ticks. You can thereafter convert to milliseconds as shown below:
long dateticks = DateTime.Now.Ticks;
long datemilliseconds = dateticks / TimeSpan.TicksPerMillisecond;
DateTime[] dates = ;
var minDate = dates.Min();
var msDates = dates.Select(date => (date - minDate).TotalMilliseconds).ToArray();

C#, how do I make a clock work with added hours?

Using Visual Studio 2008 (C#) I have to make a working clock (digital) with the current time zone hour, and a few more with different time zones, like new york, etc.
inside the form I put 2 labels (for the clocks) and a timer, inside the timer I put this code:
timer1.Interval = 1000;
label1.Text = DateTime.Now.ToLongTimeString();
DateTime myDateTime = DateTime.Now;
TimeSpan myTimeSpan = new TimeSpan(2, 0, 0);
DateTime myDateTime8 = myDateTime + myTimeSpan;
label2.Text = ("" + myDateTime8);
the part with the timespan does add 2 hours to the clock, however, instead of just the actually clock I also get the date on it's left, like for example:
"17-05-2011 22:38:00"
I need to know how can I add/subtract hours and only show the clock.
Instead of adding a timespan, simply call the AddHours method:
myDateTime.AddHours(2).ToLongTimeString();
myDateTime.ToShortTimeString() will return you only time
or as Tejs mentioned you can use ToLongTimeString() that I guess more suits your requirement.
For adding or subtracting hours you can use dateTime.AddHours(even hours in negative) or for subtracting you can also use dateTime.Subtract(time to subtract)
Using the .ToString() method of the timespan method allows you to output the date in any format you want. See http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
For your time zone needs, use an approach similar to the one suggested in this MSDN article. Notably:
Use ConvertTimeToUtc to get UTC time before performing any arithmetics.
Perform required arithmetics.
Convert back to local time using TimeZoneInfo.ConvertTime.
To get just the time part of a DateTime, use DateTime.ToShortTimeString(). Note that this is culture-aware, so if you want a fixed format, consider using DateTime.ToString() to specify a format.

Silverlight convert datePicker to DateTime

can someone tell me how to conver DatePicker value to DateTime value...
I have this variables:
DateTime now = DateTime.Now;
DatePicker rd = rd_datePicker;
I would like to get difference in days betwen this two dates, can you help me with this too?
Have you tried using DatePicker.SelectedDate?
Once you've got the two DateTime values (you may want to use DateTime.Today instead of DateTime.Now) you can subtract one from the other to get a TimeSpan, and then use TimeSpan.TotalDays to find out the number of days in the period.
(As DateTime is effectively a "local" representation, you don't need to worry about time zones or variable day lengths in this particular case. Date and time arithmetic in general is rather tricky...)
DatePicker dp = new DatePicker();
DateTime dt=new DateTime();
dp.Text = dt.Date.ToString("yyyy/MM/dd");

how to minus two time period

I am developing a c#.net app in which I need to subtract two time periods.
I have taken two date objects and subtracted them but it doesn't work.
TimeSpan can be used to measure the differences between 2 DateTimes:
DateTime dt1 = ...
DateTime dt2 = ...
TimeSpan diff = dt2 - dt1;
Check the TimeSpan struct.
Also, for DateTime, you have handy procedures such as AddDays:
DateTime later = mydate.AddDays(1.0);
Similarlly, there are AddHours, AddMonths and even AddMilliseconds:
http://msdn.microsoft.com/en-us/library/system.datetime_members.aspx
Subtracting one DateTime from another returns a Timespan object. which basically tells you how many days/hours/mins/secs/milliseconds/ticks that occured between the 2 DateTimes.

How do you calculate accumulative time in C#?

I want to calculate the time span between 2 times which I saved in a database.
So literally I want to know the length of time between the 2 values.
14:10:20 - 10:05:15 = 02:05:05
So the result would be 02:05:05.
How would I be able to achieve this using C#?
14:10:20 is the format I saved it in in my database.
Read the two time values into TimeSpan variables, then perform a .Subtract() on the bigger TimeSpan variable to get the TimeSpan result.
E.g. TimeSpan difference = t1.Subtract(t2);.
Your first step will be to get the timevalues stored in your database into .NET DateTime structs.
If you stored them as SQL-DateTime values in the database you can get them directly as DateTime. It would look something like this:
SQLCommand getTimeCommand = new SQLCommand("SELECT time FROM table", dbConnection);
SQLDataReader myReader = getTimeCommand.ExecuteReader();
while (myReader.Read())
{
DateTime time = myReader.GetDateTime(0);
}
myReader.Close();
Your implementation might differ, refer to the ADO.NET documentation in the MSDN library.
If you already got a string representing the time you can parse the string into a DateTime using the static methods
DateTime.Parse
or
DateTime.ParseExact
In your case you might need to use ParseExact, which can pe provided with a format-string defining how to read the string. Examples should be found in the MSDN library.
Durations in .NET are stored inside a TimeSpan struct. Getting the elapsed time between to datetimes is easy:
DateTime time1, time2; //filled with your timevalues from the db
TimeSpan elapsed = d2 - d1;
elapsed now contains the timespan between the two DateTimes. There are several members for the struct to access the TimeSpan. Look into the MSDN-Library to find the ones you need.
DateTime objects support the "-" operator so you can just read your times into such objects and subtract them. To test, check this out:
DateTime then = DateTime.Now;
Thread.Sleep(500);
DateTime now = DateTime.Now;
TimeSpan time = now - then;
MessageBox.Show(time.ToString());

Categories