I am using a for loop with date range in C#.
for (var date = fromDate;date <= toDate; date.Value.AddMonths(1))
However, I found out that the date value is not added in the looping. Is that the AddMonths is not appropriate in the for loop? How should I add the months in the for loop?
DateTime values in c# are immutable. That is, after they are created, they can't be changed.
What you can do, is assign a new value to the date variable.
for (var date = fromDate.Value; date <= toDate; date = date.AddMonths(1))
{
// do something with date
}
Related
I have a date value that I want to strip the time from. I want the return type to be a date type since I want to order the list of date I have. having a list to string representing Date does not return a correct order.
I know that DateTime always returns the date with the time. What are my options here? How can I better my code and have a list of items of Date type without the time?
Edit: I would like to have the date only. nothing after that. Something like 8/7/2016 not 8/7/2016 00:00:00 or anything after date. and in a date object.
Here is my code:
using (var db = new MyModel.Context())
{
var cert = (
from tr in db.uspTranscripts(personId)
from a in db.PersonTranscripts.Where(x => x.UPID == personId)
from b in db.LU_CreditType.Where(x => x.ID == a.CreditTypeID)
select new CertViewModel
{
ActivityTitle = tr.ActivityTitle,
Score = tr.Score,
Status = tr.Status,
CompletionDate = tr.CompletionDate,
CretitTypeName = b.ShortName,
CompletedDateSorted = a.HK_CreatedOn
}).OrderByDescending(x => x.CompletedDateSorted).ToList();
List<CertViewModel> certlist = cert;
foreach (var item in certlist)
{
string itemWithoutHour = item.CompletionDate.Value.ToShortDateString();
var itemConverted = DateTime.ParseExact(itemWithoutHour, "M/d/yyyy", null);
item.CompletionDate = itemConverted;
}
return certificateslist.GroupBy(x => x.ActivityTitle).Select(e => e.First()).ToList();
}
For any given DateTime object, you can reference its Date property to strip out the time values:
var withTime = DateTime.Now; // 8/7/2016 22:11:43
var withoutTime = withTime.Date; // 8/7/2016 00:00:00
The .NET framework does not have a date-only object.
It may be worth understanding how the DateTime structure works. Internally, it stores an offset in ticks (1 tick = 100 nanoseconds) since 1/01/0001 12:00 am in a single 64-bit unsigned integer. (1 tick = 100 nanoseconds)
The DateTime structure then provides many useful methods and properties for dealing with dates and times, such as adding some days to an existing date, or calculating the difference of two times. One useful property is Date, which rounds a DateTime object down to the nearest day (12:00 am).
Dates, times and dates-with-times are all very similar, the main difference is how you format them, a date-with-time where you omit the time is just a date.
What David has suggested is that you work with the DateTime structure internally, strip any times using the Date property, sort on the DateTime, compare them and modify them as DateTime objects.
Only convert them to a string when they need to be displayed, at which point you can use methods such as ToShortDateString() or ToString(string format) to display only the date.
What's wrong with the following code? The date is not incrementing in my FOR loop so it goes into an endless loop because the condition is never met. I also tried it with a WHILE loop and got the same result.
var startDate = DateTime.Today;
var endDate = new DateTime(2016, 12, 31);
for (var date = startDate; date <= endDate; date.AddDays(1))
{
// Some logic here
}
AddDays doesn't modifies value of date, it just returns new instance of DateTime, and you're not assigning back incremented value.
It should be
for (var date = startDate; date <= endDate; date = date.AddDays(1))
From the documentation:
Returns a new DateTime that adds the specified number of days to the
value of this instance.
So, date.AddDays(1) doesn't change the value of date; it returns a new DateTime representing the changed value. If you want to change date, do this:
date = date.AddDays(1);
I want to compare two dates; one taken from a Date column in SQL and the current DateTime.Now. The former has no time portion (technically it does, but it's zeroed out) and of course the later will have the current time to the nearest millisecond. Here is what I am doing now, and it seems inefficient:
DateTime compareDate = Convert.ToDateTime(string.Format("{0:M/d/yyyy}", DateTime.Now));
if (myObj.EndDate < compareDate)
{
myObj.Status = "PAST";
}
else if (myObj.StartDate <= compareDate && myObj.EndDate >= compareDate)
{
myObj.Status = "ACTIVE";
}
else
{
myObj.Status = "PENDING";
}
Is there a better way to strip time off a DateTime variable?
Yes, use the Date property of the DateTime structure, or just use DateTime.Today.
e.g.
DateTime compareDate = DateTime.Now.Date
or
DateTime compareDate = DateTime.Today
Use the property "Date" on the the DateTime variable you want to strip the time from.
var pureDate = DateTime.Now.Date;
I need to compare a cell's value to dates in the scope of the current week.
I need to see if a date from a cell can be matched to any date in the current week. If so, success Match should be incremented.
I was thinking about putting dates in an array or dictionary and then compare it to a cell value.
DateTime cellValue = DateTime.Now;
var beginweek = DateTime.Now.Date.AddDays( (int)DateTime.Now.DayOfWeek *-1);
var endweek = beginweek.AddDays(6);
if (cellValue.Date >= beginweek.Date && cellValue.Date <= endweek.Date)
{
//do something
}
You can use the Week class of the Time Period Library for .NET:
// ----------------------------------------------------------------------
public bool IsInCurrentWeek( DateTime test )
{
return new Week().HasInside( test );
} // IsInCurrentWeek
I fixed this with a solution based on Jeremy's answer. The difference is that I used
DateTime cellDateValue = Convert.ToDateTime(((HtmlCell)cell).InnerText);
instead of
DateTime cellValue = DateTime.Now;
I have the following code:
DataTable t = new DataTable();
t.Locale = CultureInfo.InvariantCulture;
t.Columns.Add("Date", typeof(DateTime));
DateTime today = DateTime.Now;
DateTime yesterday = today.AddDays(-1);
DateTime tomorow = today.AddDays(1);
t.Rows.Add(yesterday);
t.Rows.Add(today);
t.Rows.Add(tomorow);
string filter = string.Format(CultureInfo.InvariantCulture,
"Date >= #{0}# AND Date <= #{1}#", yesterday, tomorow);
t.DefaultView.RowFilter = filter;
foreach (DataRowView v in t.DefaultView)
Console.WriteLine(v["date"]);
I'm expecting that the filtered t.DefaultView now contains all three "days". But for some reason the last date from the range isn't included. It seems <= operator for DateTime type works like a <.
Where is the problem? Is that a bug? Any suggestions how to overcome that?
Update.
Got some responses about DateTime type and comparison operators. Thanks.
But now I want to direct attention to filter expression.
Ok, say I have the folloving loop:
foreach (DataRow r in t.Rows)
{
DateTime date = (DateTime)r["Date"];
if (yesterday <= date && date <= tomorow)
Console.WriteLine(date);
}
This loop should show the same result like
foreach (DataRowView v in t.DefaultView)
Console.WriteLine(v["date"]);
from the previous example, yes? No! Here <= works as I'm expecting and the result is all three days. Why?
Update #2: solution.
As Joe has noted - the problem is about fractions of a second.
If I format upper and lower bounds with Round-trip date/time pattern (to preserve fractions of a second) - everything works just fine:
string filter = string.Format(CultureInfo.InvariantCulture,
"Date >= '{0}' AND Date <= '{1}'",
yesterday.ToString("o", CultureInfo.InvariantCulture),
tomorow.ToString("o", CultureInfo.InvariantCulture));
The date comparison takes the time into account. So, for instance, "today at midday" is greater than just "today". If you use DateTime.Now, the time is included. So, if DateTime.Now is "today at midday", then tomorrow = today.AddDays(1) is less than "tomorrow at 3PM"... So you need to ignore the time part of the date. You can do that by formatting the date without the time. Also, if you want to check that a date is "less or equal than tomorrow" (regardless of the time), check that it is "strictly less than the day after tomorrow" :
string filter = string.Format(CultureInfo.InvariantCulture,
"Date >= #{0:MM/dd/yyyy}# AND Date < #{1:MM/dd/yyyy}#",
yesterday,
tomorrow.AddDays(1));
The code you posted in your update is not equivalent to the row filter.
Your row filter formats the date using the general format for the current culture. This probably does not include fractions of a second - therefore unless you happen to call DateTime.Now on a second boundary, your tomorrow value will be some fractions of a second beyond the range specified by the filter.
I.e. if your tomorrow value is '2009-12-23 01:02:03.456', your row filter is only taking values up to and including '2009-12-23 01:02:03', a few fractions of a second before the value specified by tomorrow.
If you only want to compare dates, you should use DateTime.Date to truncate the time component from your dates (and use DateTime.Today rather than DateTime.Now for the current date).
Try with
DateTime today = DateTime.Today;
if does not solve, check whether your date field contains time also. there lies your problem.
Update: your second comment.
when you compare with DateTime.Now e.g. Date <= 21.12.2009 14:35:35, it will take all before 14:35 hours and will ignore later rows. Hope this helps you.
See following article to get more idea
http://dotnetguts.blogspot.com/2007/06/understanding-datetime-and-timespan-in.html