I am struggling to find an easy and efficient solution to calculating the week day of a month. For example, if a given date is the first Monday Monday 5th March 2018
then I want to get the date for each first Monday of a month for the next 6 months e.g: Monday 2nd April 2018
and Monday 3rd May 2018
and so on.
I have tried to use the following code from this question. However, the code below only returns the week number I would like it to return the whole date.
static class DateTimeExtensions
{
static GregorianCalendar _gc = new GregorianCalendar();
public static int GetWeekOfMonth(this DateTime time)
{
DateTime first = new DateTime(time.Year, time.Month, 1);
return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
}
static int GetWeekOfYear(this DateTime time)
{
return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
}
}
But I am confused and don't know how I can modify the above code to solve this issue. Any help will be appreciated.
Recently Microsoft has released a DateTimeRecognizer project on GitHub which allows you to parse natural language datetime into Plain Old C# DateTime Objects. It's throughly tested, so no monkey work there.
They have also released a nuget package of the same. Which you can install via the nuget package manager.
First install the nuget package:
While, all of that is out of the way. I have developed a small utility program, which first extracts the datetime from the natural language then finds the first occurrence of subsequent dates of the same day next month.
using Microsoft.Recognizers.Text;
using Microsoft.Recognizers.Text.DateTime;
using System;
using System.Collections.Generic;
using System.Linq;
namespace RecognizerDemo
{
class Program
{
static void Main(string[] args)
{
try
{
string Query = string.Empty;
Console.WriteLine("Enter date: ");
Query = Console.ReadLine();
DateTime parsedDate = ExtractDateFromNaturalLanguage(Query);
List<DateTime> futureDates = GetSubsequentDateTimes(parsedDate);
foreach (var item in futureDates)
{
Console.WriteLine(item.ToLongDateString());
}
}
catch(Exception ex)
{
Console.WriteLine($"Failed because: {ex.Message}");
}
}
static List<DateTime> GetSubsequentDateTimes(DateTime date)
{
try
{
List<DateTime> futureDates = new List<DateTime>();
DayOfWeek dayOfWeekOriginalDate = date.DayOfWeek;
int month = date.Month;
futureDates.Add(date);
for (int i = month + 1; i <= month + 5; i++)
{
DateTime dt = new DateTime(date.Year, i, 1);
while (dt.DayOfWeek != dayOfWeekOriginalDate)
{
dt = dt.AddDays(1);
}
futureDates.Add(dt);
}
return futureDates;
}
catch(Exception ex)
{
throw;
}
}
static DateTime ExtractDateFromNaturalLanguage(string Query)
{
try
{
DateTimeModel model = DateTimeRecognizer.GetInstance().GetDateTimeModel(Culture.English);
List<ModelResult> parsedResults = model.Parse(Query);
Dictionary<string, string> resolvedValue = (parsedResults.SelectMany(x => x.Resolution).FirstOrDefault().Value as List<Dictionary<string, string>>).FirstOrDefault();
string parsedDate = string.Empty;
if (resolvedValue["type"] == "daterange")
parsedDate = resolvedValue["start"];
if (resolvedValue["type"] == "date")
parsedDate = resolvedValue["value"];
DateTime parsedDateTimeObject = DateTime.Parse(parsedDate);
return parsedDateTimeObject;
}
catch (Exception ex)
{
throw;
}
}
}
}
This, is what you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With