What I need is to get logic on how to get monthname-year between two dates.
Dictionary<Monthname,year> GetMonthsandYear(Datetime d1,Datetime d2)
or
List<Tuple<string,int> GetMonthsandYear(Datetime d1,Datetime d2)
example : jan-1-2013 to mar-3-2013
should return January-2013,february-2013,march-2013 or in reverse format by list.reverse
If your actual requirement is "the previous 24 months" then it's much simpler. Just start off with the current month, and get the 1st day of it - then iterate and add 1 month 24 times.
Personally I'd return an IEnumerable<DateTime> rather than anything else - you can format each element however you want - but it's pretty simple:
public static IEnumerable<DateTime> GetMonths(int count)
{
// Note: this uses the system local time zone. Are you sure that's what
// you want?
var today = DateTime.Today;
// Always return the 1st of the month, so we don't need to worry about
// what "March 30th - 1 month" means
var startOfMonth = new DateTime(today.Year, today.Month, 1);
for (int i = 0; i < count; i++)
{
yield return startOfMonth;
startOfMonth = startOfMonth.AddMonths(-1);
}
}
Then if you want a List<string> of these values as "February 2014" etc for example, you could have:
var monthYears = GetMonths(24).Select(dt => dt.ToString("MMMM yyyy"))
.ToList();
Note that a Dictionary<...> would not be appropriate unless you really don't care about the order - and I suspect you do. You shouldn't rely on the order in which items are returned from a Dictionary<TKey, TValue> when you view it as a sequence - it's not intended to be an ordered collection.
I don't understand why you need Dictionary or List<Tuple<string,int> but one solution could be;
DateTime dt1 = new DateTime(2013, 1, 1);
DateTime dt2 = new DateTime(2013, 3, 3);
while (dt1 < dt2)
{
Console.WriteLine(dt1.ToString("MMMM-yyyy"));
dt1 = dt1.AddMonths(1);
}
Result will be;
January-2013
February-2013
March-2013
Even if you need, you can add these values to a List<string> in while loop.
But be carefull about what Jon said, this solution will generate only January and February if your dt1.Day is greater than dt2.Day.
Related
I'm searching for a C# equivalent to the functionality provided by the boost:gregorian generators.
Specifically, I have wildcard dates and need to convert them to concrete dates.
For example a wildcard 'Third Monday in January' could be done with boost::gregorian as
typedef nth_day_of_the_week_in_month nth_dow;
nth_dow ndm(nth_dow::third, Monday,Jan);
date d = ndm.get_date(2002);
//2002-Jan-21
By using this feature, it can then be further wildcarded as 'Third Monday every month', or '.. every odd month'.
What is the best way to achieve such behaviour in C#?
It is possible to do this straight up with DateTime.DaysInMonth foreach loop then check the day, but it can be a pain so I suggest you just set up a method such as below
public static IEnumerable<DateTime> AllDatesInMonth(int year, int month)
{
int days = DateTime.DaysInMonth(year, month);
for (int day = 1; day <= days; day++)
{
yield return new DateTime(year, month, day);
}
}
Then you can do something similar to the below code:
foreach (DateTime date in AllDatesInMonth(2016,4).Where(dt => dt.DayOfWeek == DayOfWeek.Monday))
break; //This will work as it will execute the break as soon as it hits the first monday of the month
What do you mean by wildcard dates though? TimeSpans?
I have something odd requirement. I have Current Date and List of Week Days. And I want next all possible date till the target date.
For i.e. Today, its 22-04-2014 And Tuesday. Target date is 15-05-2014I have 2 week days, Monday and Thursday. So code should find near by Week Day, which will be Thursday here. So It should return date of Thursday which is 24-04-2014. Now, next turn is of Monday which comes from List. So now, It should return date of Monday which is 28-04-2014.
It should keep repeating till the target date.
So, final result will be
24-04-2014,
28-04-2014,
1-05-2014,
5-05-2014,
8-05-2014,
12-05-2014
Please help me to get this type of result. Here, Monday and Thursday is not fixed. It can be any Day and any number of Day.
Update : Link to the working example - Example
You can try this code, i have tested it and working correctly
private List<DateTime> ProcessDate(DateTime dtStartDate, DateTime targetDate)
{
DateTime dtLoop = dtStartDate;
//dtRequiredDates to hold required dates
List<DateTime> dtRequiredDates = new List<DateTime>();
for (int i = dtStartDate.DayOfYear; i < targetDate.DayOfYear; i++)
{
if (dtLoop.DayOfWeek == DayOfWeek.Monday || dtLoop.DayOfWeek == DayOfWeek.Thursday)
{
dtRequiredDates.Add(dtLoop);
}
dtLoop = dtLoop.AddDays(1);
}
return dtRequiredDates;
}
You may have to enhance this codes so that it doesn't throw any exception based on the requirement.
UPDATE 2:
You can have another method which will accept the days of week as follows
private List<DateTime> ProcessDate(DateTime dtStartDate, DateTime targetDate, List<DayOfWeek> daysOfWeek)
{
DateTime dtLoop = dtStartDate;
List<DateTime> dtRequiredDates = new List<DateTime>();
for (int i = dtStartDate.DayOfYear; i < targetDate.DayOfYear; i++)
{
foreach (DayOfWeek day in daysOfWeek)
{
if (dtLoop.DayOfWeek == day)
{
dtRequiredDates.Add(dtLoop);
}
}
dtLoop = dtLoop.AddDays(1);
}
return dtRequiredDates;
}
Here is the Example
Hence you can pass any number of week days as you wish.
Hope this helps
You could try something like this:
List<DayOfWeek> listOfDays = new List<DayOfWeek>{DayOfWeek.Monday, DayOfWeek.Thursday};
var end = new DateTime(2014,05,15);
var day = DateTime.Now.Date;
while (day < end)
{
day.AddDays(1); // adds +1 days to "day"
if (listOfDays.Contains(day.DayOfWeek)) Console.WriteLine(day.Date.ToString());
}
(I can't test the code right now, so maybe you need to modify a little ;-)
I'm trying to get the number of days (calculated byu datediff) in sql and the number of days in c# (calculated by DateTime.now.Substract) to be the same, but they return different results....
//returns 0
int reso = DateTime.Now.Subtract(expirationDate).Days;
vs
//returns 1
dateDiff(dd,getDate(),ExpirationDate)
In both cases, ExpirationDate is '10/1/2011 00:00:00', and the code and the DB are sitting on the same server. I want the return int to be the same. I suspect I'm missing something stupid... ideas??
dateDiff(dd,getDate(),ExpirationDate) Is doing a days comparison. DateTime.Now.Subtract(expirationDate).Days is doing a date and time
For example
SELECT dateDiff(dd,'10/1/2011 23:59:00' , '10/2/2011') returns one day even when only one minute apart.
If you want the same in C# you need to remove the time component
e.g.
DateTime dt1 = new DateTime(2011,10,1, 23,59,0);
DateTime dt2 = new DateTime(2011,10,2, 0,0,0);
Console.WriteLine((int) dt2.Subtract(dt1.Subtract(dt1.TimeOfDay)));
So in your case it would be something like
DateTime CurrentDate = DateTime.Now;
int reso = CurrentDate.Subtract(CurrentDate.TimeOfDay).Subtract(DateTime.expirationDate).Days;
I haven't tested it but I would not do
DateTime.Now.Subtract(DateTime.Now.Subtract.TimeOfDay)
Because the second call to Now wouldn't be guaranteeing to be the same as first call to Now
In any case Stealth Rabbi's answer seems more elegant anyway since you're looking for a TimeSpan not a DateTime
10/1/2011 is less than 1 day away from DateTime.Now. Since you're getting back a TimeSpan and then applying Days to it, you're getting back a TimeSpan that is < 1 day. So it'll return 0 Days.
Instead, just use the Date component of those DateTimes and it'll correctly report the number of days apart - like this:
DateTime now = DateTime.Now;
DateTime tomorrow = new DateTime(2011, 10, 1);
var val = (tomorrow.Date - now.Date).Days;
This will yield you 1 day.
I'm assuming you want the number of Total days, not the number of days from the largest previous unit. You'd want to use the TotalDays property. Also, you may find it easier to use the minus operator to do a subtraction
DateTime d1 = DateTime.Now;
DateTime d2 = new DateTime(2009, 1, 2);
TimeSpan difference = d1 - d2;
Console.WriteLine(difference.TotalDays); // Outputs (today):1001.46817997424
Goal - find out which DateTime is more recent.
Can I figure this out with this code:
DateTime dt1 = new DateTime(...); //let's say it was created on 1/1/2000
DateTime dt2 = new DateTime(...); //let's say it was create on 1/1/2011
if (dt2.ToBinary() > dt1.ToBinary()) {
print dt2 is newer than dt1 }
Can I simply convert the DateTime objects to binary, then presume that the larger one is more recent?
Thanks,
Kevin
if (dt2 > dt1) {
print dt2 is newer than dt1 }
should be enough as DateTime overloads the comparison operators.
You can usually do better than that:
if (dt2 > dt1)
The tricky bit is taking time zones into consideration... you can potentially use
if (dt2.ToUniversalTime() > dt1.ToUniversalTime())
but only if you know that any "local" times really are local in the system's time zone.
Dates and times in .NET are a bit of a mess :(
No, you can't. ToBinary() returns an internal format that also stores time zone info.
Instead, you can compare the DateTimes directly:
if (dt2 > dt1)
DateTime overloads the comparison operators.
You could compare the Ticks property, but you shouldn't bother.
You can use Datetime.Compare
int iDiff = DateTime.Compare(new DateTime(2011, 02, 28), new DateTime(2011, 01, 30));
-1 = The Fisrt Date is Less than the Second
0 = The First Date is equal than the Second
1 = The First Date is Greater than the Second
DateTime objects are comparable themselves, so
if (dt1>dt2)
Console.WriteLine('dt1 is newer');
else if (dt1>dt2)
Console.WriteLine('dt2 is newer');
else // they are equal
Console.WriteLine('dt1 and dt2 are the same');
will work too.
In addition DateTime implements the IComparable<DateTime> interface, so you can do:
int result = dt1.CompareTo(dt2);
if (result > 0)
Console.WriteLine('dt1 is newer');
else if (result < 0)
Console.WriteLine('dt2 is newer');
else // result = 0
Console.WriteLine('dt1 and dt2 are the same');
EDIT: This ignores time zones, local times, etc...
I have the following snippet that I use to get the individual dates between two dates:
DateTime[] output = Enumerable.Range(0, 1 + endDate.Subtract(startDate).Days)
.Select(offset => startDate.AddDays(offset))
.ToArray();
However, the following section
endDate.Subtract(startDate).Days
does not have a .Months to return the months in the date range.
For example, if I provide 1/1/2010 and 6/1/2010 I would expect to return 1/1/2010, 2/1/2010, 3/1/2010, 4/1/2010, 5/1/2010 and 6/1/2010.
Any ideas?
Try this:
static IEnumerable<DateTime> monthsBetween(DateTime d0, DateTime d1)
{
return Enumerable.Range(0, (d1.Year - d0.Year) * 12 + (d1.Month - d0.Month + 1))
.Select(m => new DateTime(d0.Year, d0.Month, 1).AddMonths(m));
}
This includes both the starting month and the ending month. This finds how many months there is, and then creates a new DateTime based on d0´s year and month. That means the months are like yyyy-MM-01. If you want it to includes the time and day of d0 you can replace new DateTime(d0.Year, d0.Month, 1).AddMonths(m) by d0.AddMonths(m).
I see you need an array, in that case you just use monthsBetween(..., ...).ToArray() or put .ToArray() inside the method.
Since I just needed the year and month in between two dates I modified Lasse Espeholt answer a little. suppose:
d0 = 2012-11-03
d1 = 2013-02-05
The result will be something like this:
2012-11
2012-12
2013-01
2013-02
private List<Tuple<int,int>> year_month_Between(DateTime d0, DateTime d1)
{
List<DateTime> datemonth= Enumerable.Range(0, (d1.Year - d0.Year) * 12 + (d1.Month - d0.Month + 1))
.Select(m => new DateTime(d0.Year, d0.Month, 1).AddMonths(m)).ToList();
List<Tuple<int, int>> yearmonth= new List<Tuple<int,int>>();
foreach (DateTime x in datemonth)
{
yearmonth.Add(new Tuple<int, int>(x.Year, x.Month));
}
return yearmonth;
}
Is this what you are looking for? The requirement is very ambiguous.
DateTime[] calendarMonthBoundaries = Enumerable.Range(0, 1 + endDate.Subtract(startDate).Days)
.Select(offset => startDate.AddDays(offset))
.Where(date => date.Day == 1)
.ToArray();
You could enumerate increments of months with:
private static IEnumerable<DateTime> ByMonths(DateTime startDate, DateTime endDate)
{
DateTime cur = startDate;
for(int i = 0; cur <= endDate; cur = startDate.AddMonths(++i))
{
yield return cur;
}
}
and then call ToArray() on that if you want an array. It's reasonably good about having values that are likely to be what is wanted; e.g. if you start at Jan 31st you'll next get Feb 28th (or 29th on leap years), then Mar 31st, then Apr 30th and so on.