C# Check given date and time - c#

Good day everyone. I'm new to C# but I can't seem to understand how DateTime work.
All I wanted to do is to check If a (givenday) = today and time is 7pm I wanted to return true. Is this the right way to do it?
Take note ActionDate is a field which is inputed by the user.
DateTime dateA = Convert.ToDateTime(ActionDate);
int a = dateA.Year;
int b = dateA.Month;
int c = dateA.Day;
int d = timeA.Hour;
int e = timeA.Minute;
var newDate = new DateTime(a, b, c, d, e, 0);
DateTime end = Convert.ToDateTime(newDate);
DateTime start = Convert.ToDateTime(A);
TimeSpan span = end.Subtract(start);
Decimal minutes = Convert.ToDecimal(span.TotalMinutes);
if
{
return true;
} else
{
return false;
}
Thank you in advance.

The way to check if a give date is today and is at 7pm is to use DateTime.Now.
Note that 19 is 7pm and 7 is 7am, the Hour property uses 24 hour format.
bool IsCurrentDayAnd7(DateTime dt) => dt.Date == DateTime.Now.Date && dt.Hour == 19;
As #TimSchmelter commented you could use DateTime.Today:
bool IsCurrentDayAnd7(DateTime dt) => dt.Date == DateTime.Today && dt.Hour == 19;

You can use Date property to compare date with current date.
if (newDate.Date == DateTime.Now.Date && newDate.Hour == 19)
{
return true;
}

You have made your code a bit too complicated. First, convert that user input to date, and compate it with current date and time.
DateTime dateA = Convert.ToDateTime(ActionDate);
if (dateA.Date == DateTime.Today && dateA.Hour == 19)
{
//it is current date and hour is 7pm
}
Alternatively, check if user's imput is ok, like this:
DateTime dateA;
if (!DateTime.TryParse(ActionDate, out dateA))
{
//alert user that he's entered wrong date
}
EDIT:
as Tim Schmelter noted, code's a bit more readable using DateTime.Today instead of DateTime.Now.Date

Related

Checking if date is within a date range

I have a GridView and I want to change the colour of the row if the date in a column is within a certain date range.
DateTime dt = new DateTime();
if (DateTime.TryParse(c.Text, out dt))
{
if (dt.Date >= DateTime.Now.AddDays(Mod.ValidUntilDays).Date && dt.Date <= DateTime.Now.AddDays(Mod.ValidUntilDays).Date)
{
e.Item.Style.Add("background-color", "#FF0303");
}
}
So the value coming from the grid is dt.Date is equal to 23/09/2016 00:00:00 and the date range I want to check is 25/09/2016 00:00:00. Mod.ValidUntilDays just adds 5 days to Today's date.
So what I am trying to do is check is23/09/2016 00:00:00 within the 5 day range which it is but the code never goes into the if statement.
you are asking the date to be >= 5-days-time and <= 5-days-time. So unless it == 5-days-time it'll return false. I think you mean this:
DateTime dt = new DateTime();
if (DateTime.TryParse(c.Text, out dt))
{
DateTime now = DateTime.Now;
if (dt.Date >= now.Date && dt.Date <= now.AddDays(Mod.ValidUntilDays).Date)
{
e.Item.Style.Add("background-color", "#FF0303");
}
}

Use LINQ to compare the date part of DateTime

I want to filter some documents between a different date. First I tried comparing the dates directly, but the time (hour, minutes, second) doesn't have to be considered. Therefore only the date part is needed, but the following approach is wrong:
DateTime? fromDate = documentFilter.fromDate;
if (fromDate.HasValue) {
filterResults = filterResults.Where (d => d.LastModifiedAt.Value.Year >= fromDate.Value.Year
&& d.LastModifiedAt.Value.Month >= fromDate.Value.Month
&& d.LastModifiedAt.Value.Day >= fromDate.Value.Day);
}
DateTime? toDate = documentFilter.toDate;
if (toDate.HasValue) {
filterResults = filterResults.Where (d => d.LastModifiedAt.Value.Year <= toDate.Value.Year
&& d.LastModifiedAt.Value.Month <= toDate.Value.Month
&& d.LastModifiedAt.Value.Day <= toDate.Value.Day);
}
Consider the from date 8/15/2014 12:00:00 AM and the to date 9/15/2014 12:00:00 AM. If the document has the date 8/16/2014 10:06:25 AM it won't be in the results. The reason is that I directly compare each component (year, month, day). Because the day is 16 and 16 > 15 the last condition is not met.
How can I solve this? Should I set the time to one minute before midnight? Or should I calculate the difference?
Just use the DateTime.Date property:
if (fromDate.HasValue) {
filterResults = filterResults
.Where(d => d.LastModifiedAt.Date >= fromDate.Value.Date);
}
if (toDate.HasValue) {
filterResults = filterResults
.Where(d => d.LastModifiedAt.Date <= toDate.Value.Date);
}
DateTime has a Date property which returns a DateTime for the same day at midnight:
DateTime? fromDate = documentFilter.fromDate;
if (fromDate.HasValue)
filterResults = filterResults.Where(d => d.LastModifiedAt.Value.Date >= fromDate.Value.Date);
DateTime? toDate = documentFilter.toDate;
if (toDate.HasValue)
filterResults = filterResults.Where(d => d.LastModifiedAt.Value.Date <= toDate.Value.Date);

How do I determine if a date lies between current week dates?

In C#,
How we are check certain date with in week dates?
Eg: 6/02/2014
Current Weeks: 02/02/2014 - 08/02/2014
so this dates are with in above week....
Use this for check (last parameter is optional if you want always 1 week from fromDate, you don't need use last parameter):
public static bool DateInside(DateTime checkDate,
DateTime fromDate, DateTime? lastDate = null)
{
DateTime toDate = lastDate != null ? lastDate.Value : fromDate.AddDays(6d);
return checkDate >= fromDate && checkDate <= toDate;
}
To call use:
bool isDateInside = DateInside(new DateTime(2014, 02, 06),
new DateTime(2014, 02, 02)); // return true
And search first :) Answer is also here: How to check whether C# DateTime is within a range
If you want to check if the dates are inside the same week, then you can use this:
public static bool DateInsideOneWeek(DateTime checkDate, DateTime referenceDate)
{
// get first day of week from your actual culture info,
DayOfWeek firstWeekDay = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
// or you can set exactly what you want: firstWeekDay = DayOfWeek.Monday;
// calculate first day of week from your reference date
DateTime startDateOfWeek = referenceDate;
while(startDateOfWeek.DayOfWeek != firstWeekDay)
{ startDateOfWeek = startDateOfWeek.AddDays(-1d); }
// fist day of week is find, then find last day of reference week
DateTime endDateOfWeek = startDateOfWeek.AddDays(6d);
// and check if checkDate is inside this period
return checkDate >= startDateOfWeek && checkDate <= endDateOfWeek;
}
Actual week in my culture info start with monday, February 3th 2014 (so for me is week between February 3th and February 9th). If I check any date with reference date (second parameter) as today (2014-Feb-06) I get this results:
For 2014-Feb-02 (Sunday before this week): false
For 2014-Feb-03 (Monday inside this week): true
For 2014-Feb-06 (Today inside this week): true
For 2014-Feb-09 (Sunday inside this week): true
For 2014-Feb-10 (Monday next week): false
You can call this method to check if one date is inside the same week as referentional like this:
DateInsideOneWeek(new DateTime(2014, 02, 02), new DateTime(2014, 02, 06));
You can find current week start and end dates with this code:
DateTime startDateOfWeek = DateTime.Now.Date; // start with actual date
while(startDateOfWeek.DayOfWeek != DayOfWeek.Monday) // set first day of week in your country
{ startDateOfWeek = startDateOfWeek.AddDays(-1d); } // after this while loop you get first day of actual week
DateTime endDateOfWeek = startDateOfWeek.AddDays(6d); // you just find last week day
Is this you wanted?
hmm
public bool isBetween(DateTime input, DateTime date1, DateTime date2)
{
if (input > date1 && input < date2)
return true;
else
return false;
}
?
input= your date
date1 & date2 = start and end of a week
How about:
bool inRange = (date >= lowerDate && date <= upperDate);
Here's another solution :)
public static class DateExtensions
{
private static void Swap<T>(ref T one, ref T two)
{
var temp = one;
one = two;
two = temp;
}
public static bool IsFromSameWeek(this DateTime first, DateTime second, DayOfWeek firstDayOfWeek = DayOfWeek.Monday)
{
// sort dates
if (first > second)
{
Swap(ref first, ref second);
}
var daysDiff = (second - first).TotalDays;
if (daysDiff >= 7)
{
return false;
}
const int TotalDaysInWeek = 7;
var adjustedDayOfWeekFirst = (int)first.DayOfWeek + (first.DayOfWeek < firstDayOfWeek ? TotalDaysInWeek : 0);
var adjustedDayOfWeekSecond = (int)second.DayOfWeek + (second.DayOfWeek < firstDayOfWeek ? TotalDaysInWeek : 0);
return adjustedDayOfWeekSecond >= adjustedDayOfWeekFirst;
}
}
Upd: it appears to have at least twice better performance than #Atiris solution :)

Run through loop for week and don't select weekends

So the user can select 2 different dates, a start date and an end date from 2 calendars, when they click add the dates between&including the dates selected will be added to the Database, each date as a separate record.
This works fine however I don't want weekends to be added to the Database.
I've updated the UI of datepicker http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerCustomCellRender.html
but if a user selects fri-mon for example fri,sat,sun and mon will be added to the Database.
I've tried to only run the code if datyofweek is not saturday or sunday
public ActionResult listHolidays(Holiday holiday, int? PersonId, string HolidayDate, string endDate)
{
DateTime startDates = Convert.ToDateTime(HolidayDate),
endDates = Convert.ToDateTime(endDate);
while (startDates <= endDates)
{
if (startDates.DayOfWeek != DayOfWeek.Saturday || startDates.DayOfWeek != DayOfWeek.Sunday)
{
Holiday holiday1 = new Holiday();
holiday1.PersonId = PersonId.Value;
holiday1.HolidayDate = startDates;
db.Holidays.AddObject(holiday1);
db.SaveChanges();
startDates = startDates.AddDays(1);
}
}
return RedirectToAction("Index");
}
any help?
Thanks
You need to use AND instead of OR:
if (startDates.DayOfWeek != DayOfWeek.Saturday && startDates.DayOfWeek != DayOfWeek.Sunday)
Your current condition will always be true. If startDates is DayOfWeek.Saturday, it is not DayOfWeek.Sunday and vice versa.
After that, you need to put the startDates = startDates.AddDays(1); outside the if.
Otherwise, you would have an endless loop, because as soon as startDates is DayOfWeek.Saturday, it will no longer be incremented.
Change
if (startDates.DayOfWeek != DayOfWeek.Saturday || startDates.DayOfWeek != DayOfWeek.Sunday)
To
if (startDates.DayOfWeek != DayOfWeek.Saturday && startDates.DayOfWeek != DayOfWeek.Sunday)
You'll then need to move this line
startDates = startDates.AddDays(1);
To outside of the if block (otherwise the startDates variable will never change if the date is a weekend date).
If statement is wrong and day increment in wrong place.
while (startDates <= endDates)
{
if (startDates.DayOfWeek != DayOfWeek.Saturday **&&** startDates.DayOfWeek != DayOfWeek.Sunday)
{
Holiday holiday1 = new Holiday();
holiday1.PersonId = PersonId.Value;
holiday1.HolidayDate = startDates;
db.Holidays.AddObject(holiday1);
db.SaveChanges();
}
**startDates = startDates.AddDays(1);**
}
Just as an alternative solution for you for calculating how many week days you have selected
List<DateTime> allDates = New List<DateTime>();
for (DateTime date = startingDate; date <= endingDate; date = date.AddDays(1))
allDates.Add(date);
var days = allDates.Where(x => x.IsWeekday()).Count();
Using the following DateTime extension.
public static class DateTimeExtensions
{
public static bool IsWeekday(this DateTime dt)
{
return dt.DayOfWeek != DayOfWeek.Sunday && dt.DayOfWeek != DayOfWeek.Saturday;
}
}
As per my comment, you can extend this to an IsWorkday if it is for a holiday system and encorporate some logic for checking if the day is a bank holiday.

How do I get the missing dates from a range

I'm doing this test-app and what it has to do is go through a long list of dates from a text file and get the missing ones (excluding weekend days) and write the results to an output file. If the missing date is a single day the output should be ccyy/mm/dd , if it's more than one day it should be ccyy/mm/dd - ccyy/mm/dd , so this is what I've come up with and it doesn't seem to work as it should, I think I'm not doing the test right.
List<string> missigDateStrings = new List<string>();
for (int i = 0; i < dateList.Count; i++ )
{
DateTime firstDate = dateList[i];
DateTime secondDate = dateList[i + 1];
if (firstDate.DayOfWeek != DayOfWeek.Saturday &&
firstDate.DayOfWeek != DayOfWeek.Sunday)
{
if (secondDate.DayOfWeek != DayOfWeek.Saturday &&
secondDate.DayOfWeek != DayOfWeek.Sunday)
{
if (firstDate.AddDays(1) != secondDate)
{
string sFirstMissingDate = firstDate.ToShortDateString();
DateTime testDate = firstDate;
while (testDate != secondDate)
{
testDate.AddDays(1);
if (testDate == secondDate)
{
string sLastMissingDate = firstDate.AddDays(1).ToShortDateString();
string range = String.Format("{0}-{1}", sFirstMissingDate, sLastMissingDate);
missigDateStrings.Add(range);
}
}
}
}
}
}
}
Any help would be appreciated.
P.S. all the dates have been converted to DateTime
EXCELLENT ! THANKS ALL
If the gap is longer than 7 days you get weekend days also. I assume that is your problem. So you should add another check for Sat/Sun in
while (testDate != secondDate)
and break the loop on Friday, skip weekend and start loop on Monday again.
EDIT:
Below is always false in your case probably. That's why you don't get any output.
if (firstDate == secondDate)
Change
testDate.AddDays(1);
to
testDate = testDate.AddDays(1);
DateTime is an immutable value-type, AddDays() returns a new instance but does not (cannot) change the original value.
And just for fun, to answer the title-question,
var missing = Enumerable.Range(0, 10)
.Select(i => baseDate.AddDays(i))
.Except(datesFromFile);

Categories

Resources