apekhsa
apekhsa

Reputation:

Calculate time between 9:00 AM -- 6:00 PM

I'm having a string with 8:00 AM. Suppose I want to check that time 8:00 AM comes before 9:00 Am -- 6:00 PM.
How to check this or if time 11:00 AM comes in between time 9:00 AM -- 6:00 PM? How to find it out in c#?

Upvotes: 1

Views: 2978

Answers (8)

Denis Troller
Denis Troller

Reputation: 7501

You can try to parse your string into a TimeSpan object using TimeSpan.Parse or TimeSpan.TeyParse.

If the parse succeeds, you'll have a TimeSpan object that can be compared to other timespan objects using the standard comparison operators. You can also carry out operations such as addition and substraction to calculate time between two timespans or to calculate a new time based on a time and duration.

Timespan is a very nice little class, please do not reinvent the wheel.

Update

As remarked by Cerebrus in the comment, TimeSpan is kind of weird in the sense that it can represent both a point in time and a duration. (The DateTime.TimeOfDay property is a Timespan even though it is a point in time). It is perhaps a better idea to create DateTime objects with a default date part and use this as the basis of calculations. The intent of the code will be clearer. Operations such as comparisons, additions and substractions are also available on DateTimes and yield DateTimes or TimeSpan where applicable.

Upvotes: 0

Patrick McDonald
Patrick McDonald

Reputation: 65431

String s = "8:00 AM";
DateTime dt = DateTime.Parse(s);

if (dt < DateTime.Parse("9:00 AM"))
{
    Console.WriteLine("Before");
}
else if (dt <= DateTime.Parse("6:00 PM"))
{
    Console.WriteLine("Between");
}
else
{
    Console.WriteLine("After");
}

Upvotes: 2

Cerebrus
Cerebrus

Reputation: 25775

I don't see the problem casting the strings to DateTime variables (as they should be) and then using the Compare method to perform the comparison.

Additionally, the DateTime structure implements operators such as >, <, >=, <= which simplify comparisons.

Upvotes: 0

Terry_Brown
Terry_Brown

Reputation: 1408

what about

int _dateTimeCompare = DateTime.Compare(DateTime1, DateTime2);

and then _dateTimeCompare < 1 = DateTime 1 is less than DateTime2, 0 = they're the same, > 0 = DateTime2 > DateTime1

So you can then do the comparisons.

A simple is _myDate between _date1 and _date2 would then be:

if (DateTime.Compare(_myDate, _date1) >= 0 && DateTime.Compare(_myDate, _date2) <= 0)
{
    // we're between _date1 and _date2
}

kinda thing :)

Upvotes: 0

Scott Weinstein
Scott Weinstein

Reputation: 19117

You can use the DateTime object, its static parse method, and comparison operators. Something like the following:

newTime = DateTime.Parse("8:00 AM");
fixedTime = DateTime.Parse("11:00 AM");
if (newTime < fixedTime)
{
// do something
}

If needed, you can subtract one DateTime from another to get a TimeSpan - that is a duration.

Upvotes: 7

Anton Gogolev
Anton Gogolev

Reputation: 115749

DateTime beginDate = DateTime.Today.AddHours(9);
DateTime endDate = DateTime.Today.AddHours(18);

TimeSpan diff = endDate - beginDate;

Now diff contains difference between begin date and end date.

Upvotes: 0

GWLlosa
GWLlosa

Reputation: 24403

The function DateTime.Parse() can be used to turn strings into "DateTime" objects.

You have your start time: 9:00AM.

You have your end time: 6:00PM.

You therefore have your distance between the two:

6:00PM - 9:00AM.

That will give you a TimeSpan object.

Then you take your 'mystery time', and do:

X:XX - 9:00AM.

If the resulting timespan is > 0 AND the resulting timespan is < 6:00PM-9:00AM tiemspan, you're good.

Alternately, you can simply do

if (myTime > 9:00AM && myTime < 6:00PM)

Upvotes: 0

Ed Guiness
Ed Guiness

Reputation: 35267

The general method would be to convert the time to a number that you can then use to compare.

So 8:00 AM would be simply 8, 6:00 PM would be 18, therefore 18 > 8 etc.

Upvotes: 1

Related Questions