ChineseLunisolarCalendar to GregorianCalendar invalid date - c#

I try to calculate the "Mid-Autumn Festival" public holiday in china for any year. But the date is for some years invalid, i use the documentation of wikipedia (15th day of 8th Lunisolar month)
var chineseCalendar = new ChineseLunisolarCalendar();
var midAutumnFestival = chineseCalendar.ToDateTime(year, 8, 15, 0, 0, 0, 0);
The date in the year 2017 and 2020 are invalid, what is wrong?
Calculated Date | Correct Date
2020.09.02 | 2020.10.01
2017.09.05 | 2017.10.04

2017 and 2020 have leap months.
https://www.chinesegenderchart.info/leapmonth.php
Leap 6th month for 2017
Leap 4th month for 2020
Now look into below documentation (https://msdn.microsoft.com/en-us/library/system.globalization.chineselunisolarcalendar%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396)
A leap month can occur after any month in a year. For example, the
GetMonth method returns a number between 1 and 13 that indicates the
month associated with a specified date. If there is a leap month
between the eighth and ninth months of the year, the GetMonth method
returns 8 for the eighth month, 9 for the leap eighth month, and 10
for the ninth month.
Based on above documentation, when you query for 8th month in 2017 and 2020, you have to ask for 9 as month which represents 8th month.
something like below:
var chineseCalendar = new ChineseLunisolarCalendar();
var midAutumnFestival = chineseCalendar.ToDateTime(year, 9, 15, 0, 0, 0, 0);

Related

Get same day of same week in last year

I was looking for a way to fetch the same day of the current week as a year ago. For example, today is:
August 10th 2022 - Wednesday.
Assume this is the check-in date, the check-out date I expect to get is:
August 11, 2021 - Wednesday.
Because it's the same day (Wednesday) as last year. But I need to take leap years into account, so I need to see if the current year is a leap year and if it is, if it has passed the 29th of February, the same with the date last year.
How to do this using .net core ? I thought of something like:
private DateTime GetDayOneYearBefore()
{
if(DateTime.IsLeapYear(DateTime.Today.Year) && DateTime.Today.Month > 2){
return DateTime.Today.AddDays(-365);
}
else if(DateTime.IsLeapYear(DateTime.Today.Year) && DateTime.Today.Month <= 2){
return DateTime.Today.AddDays(-364);
}
}
Since you mention the "same week" I suppose you want to get the same day of the week in the same week number?
If so, you can do the following:
// In the System.DayOfWeek enum Sunday = 0, while Monday = 1
// This converts DateTime.DayOfWeek to a range where Monday = 0 and Sunday = 6
static int DayOfWeek(DateTime dt)
{
const int weekStart = (int)System.DayOfWeek.Monday;
const int daysInAWeek = 7;
return (daysInAWeek - (weekStart - (int)dt.DayOfWeek)) % daysInAWeek;
}
var calendar = CultureInfo.CurrentCulture.Calendar;
var weekNum = calendar.GetWeekOfYear(DateTime.Today, CalendarWeekRule.FirstFourDayWeek, System.DayOfWeek.Monday);
var todayLastYear = DateTime.Today.AddYears(-1);
var lastYearWeekNum = calendar.GetWeekOfYear(todayLastYear, CalendarWeekRule.FirstFourDayWeek, System.DayOfWeek.Monday);
var sameWeekLastYear = todayLastYear.AddDays(7 * (weekNum - lastYearWeekNum));
var sameDaySameWeekLastYear = sameWeekLastYear.AddDays(DayOfWeek(DateTime.Today) - DayOfWeek(sameWeekLastYear));
As you might notice there's a little convertion method, since I normally work with Monday being the first day of the week. If you prefer a different day to be the first day of the week, simply replace System.DayOfWeek.Monday with which ever day you'd like.
See this fiddle for a test run.

Calculating for remaining days in current month [duplicate]

This question already has answers here:
How to find the first day of next month,if the current month is december
(12 answers)
Calculate difference between two dates (number of days)?
(17 answers)
Closed 5 years ago.
I would like to know how to calculate the remaining days of the month e.g (15 Feb 2017 to 28 Feb 2017) without the use of a datetimepicker set at 28 Feb 2017.
Here are my codes for subtraction of 2 datetimepicker:
DateTime startDate =
(DateTime)dateTimePicker2.Value;
DateTime endDate =
(DateTime)dateTimePicker1.Value;
TimeSpan ts = endDate.Subtract(startDate);
textBox10.Text = ts.Days.ToString();`
Here are the steps you need to go through:
take your current date
use DateTime.AddMonths() to generate a new date one month from your current date
create a new date that uses 1 for the day, and the month and year from the future date you just worked out
subtract your current date from the future date, this will give you a Timespan which contains the number of days difference
You can use the closely related question Calculate difference between two dates (number of days)? as a guide.
Here is an example, but you can use the Value property from your DateTimePicker instead. DateTime.DaysInMonth(int year, int month) is a helpful method.
DateTime beginDate = new DateTime(2017, 2, 15);
var daysLeft = DateTime.DaysInMonth(beginDate.Year, beginDate.Month) - beginDate.Day;
Console.WriteLine("days from Feb 15 to Feb 28: {0}", daysLeft);
Output:
days from Feb 15 to Feb 28: 13
You can try:
DateTime dt = DateTime.Now;
int days = dt.AddDays(1 - dt.Day).AddMonths(1).AddDays(-1).Day - dt.Day;

Comparing dates with week, month and year using .NET

I am currently doing this way to convert week of the year to milliseconds.
DateTime source = new DateTime(2014, 1, 1);
int weeks = 14; //first week of April
**int month = 4; //I don't use it yet**
int delta = 7 + DayOfWeek.Wednesday - source.DayOfWeek;
if (delta >= 7)
delta -= 7;
source = source.AddDays((weeks- 1) * 7 + delta);
double timeMs = (source - new DateTime(1970, 1, 1)).TotalMilliseconds;
Although, I came across with problems that a given week can belong to two months, just like the 14th week of 2014 belongs to March and April.
For example, I receive these entries:
week: 14, month: 3, year: 2014, total: 13
week: 14, month: 4, year: 2014, total: 98
The way I'm doing I get the same time-stamp in ms for both entries which is good for building a chart of week evolution but it is bad if I want to filter data by month and week at the same time (example: from week 14 of April to week 15 of April -> I don't want to get those 13 equipment to be related to the week 14 of April)
So my question is: given a week of the year, month and year is there any way (library or not) to compare two dates accurately with these fields?
PS: On the front-end I use moment.js to get the week of the year, month and year of a given date.
Thanks anyway.
You can use the Week class of the Time Period Library for .NET:
// ----------------------------------------------------------------------
public void WeekOfYear()
{
Week week = new Week( 2014, 14 );
Console.WriteLine( "Week: {0}", week );
Console.WriteLine( "Year: {0}, Month: {1}", week.Start.Year, week.Start.Month );
Console.WriteLine( "NextWeek: {0}", week.GetNextWeek() );
} // WeekOfYear
You can use the calendar class to get the week number http://msdn.microsoft.com/en-us/library/system.globalization.calendar(v=vs.110).aspx

Parse out monthly items from a collection of DateTime objects

I previously asked this question to take a oollection of datetime objects and group them by dayOfweek and time
So just to recap: I take a collection of DateTime
List<DateTime> collectionOfDateTime = GetDateColletion();
and then grouping by dayofWeek and time of day by doing this
var byDayOfWeek = collectionOfDateTime.GroupBy(dt => dt.DayOfWeek + "-" + dt.Hour + "-" + dt.Minute);
So at this point, I have these grouped by week (consistent time) working perfectly.
I now have a new requirement to group by Month instead of by week. When i say "month", its not the same day of the month but something like "the first tuesday of each Month"
I am trying to figure out what "key" to use in a group by to group all items that fit that monthly logic (the first tuesday of the month, the second friday of each month, etc)
As an example lets say i had these dates to start out with;
var date1 = new DateTime(2013, 1, 4).AddHours(8); // This is the first Friday in Jan
var date2 = new DateTime(2013, 2, 1).AddHours(8); // This is the first Friday in Feb
var date3 = new DateTime(2013, 1, 5).AddHours(3); // This is the first Sat in Jan
var date4 = new DateTime(2013, 2, 2).AddHours(3); // This is the first Sat in Feb
var date5 = new DateTime(2013, 2, 2).AddHours(6);  // This is the first Sat in Feb - different time
If these were the dates that went into the original array, i need a groupby to end up with 3 groups.
The first group would have date1 & date2 in it
The second group would have date3 and date4 in it.
date5 would be on its own as it doesn't match any of the other groups given the different time
Can anyone suggest anyway to group by that criteria?
I think it's easier than it looks:
var byDayOfMonth = from d in dates
let h = (d.Day / 7) + 1
group d by new { d.DayOfWeek, h } into g
select g;
Local variable h = (d.Day / 7) + 1 sets which DayOfWeek within that month it actually is.
I run it for test and received 2 groups, exactly the same as in your example. Keys for that groups are:
{ DayOfWeek = Friday, h = 1 }
{ DayOfWeek = Saturday, h = 1 }
What means, there are groups for 'First Friday of month' and 'First Saturday of month'.
You can easily extend grouping key by d.Hour and/or d.Minute if you like:
var byDayOfMonth = from d in dates
let h = (d.Day / 7) + 1
group d by new { d.DayOfWeek, h, d.Hour, d.Minute } into g
select g;
Results (keys only):
{ DayOfWeek = Friday, h = 1, Hour = 8, Minute = 0 }
{ DayOfWeek = Saturday, h = 1, Hour = 3, Minute = 0 }
{ DayOfWeek = Saturday, h = 1, Hour = 6, Minute = 0 }
There is probably an easier way to do this but this is what's come to me:
I gather from your question that you need to group everything from "the first Tuesday of February until the first Monday of March" etc. such that you get these "month" spans that are a variable number of days - depending on the month in which they start. If so then you really need to break this down into ranges using the day of the year so:
Group by the First Wednesday of the Month 2013
Group 0 (0-1)
All DayOfYear between 0 and 1 2013
Group 1 (2-36)
The first Wednesday of the month: January is DayOfYear 2.
The first Wednesday of the month: February is DayOfYear 37.
etc.
So the first range is a function f such that f(32) = 1 (DayOfYear is 32) because it falls in the range 2 to 37. This f is an indexed collection of ranges, finding the item in the collection that a given DayOfYear falls into, and returning that item's index as the group number.
You can dynamically build this table by getting your min and max dates from GetDateCollection to determine the overall range. Because the logic surrounding dates is a pretty complex topic in of itself I'd fall back on a library like NodaTime (specifically the arithmetic documentation), start with the min date, advance day by day until I found the first qualifying day (i.e., "first Monday of the month") and create a range 0 to that day - 1 as group 0 and push that onto an indexed collection (ArrayList likely). Then loop from that date using LocalDate.PlusWeeks(1) until the month changes, constructing a new range and pushing that range onto the same indexed collection.
Each time you cross into a new year you'll have to add 365 (or 366 if the previous year is a leap year) to your DayOfYear as you build your indexed collection since DayOfYear resets each year.
Now you've got a collection of ranges that acts as a table that groups days into the desired units based on their DayOfYear.
Write a function that traverses the table comparing the DayOfYear (+ [365|366] * x where x is the # of years the date you are comparing is from your min year) of a given date against the items in the collection until you locate the range that day falls within, and return that index of that item as the group number. (Alternatively each range could be a Func<DateTime,bool> that returns true if the provided DateTime falls in that range.)
An alternative data structure to the collection of ranges would be an array of ushort with length equal to all the days from min to max dates in your date range, and the value for each day their assigned group number (calculated with ranges, as above). This will perform faster for grouping, though the performance may not be noticeable if you're working with a smaller dataset (only a few hundred dates).
To group by using Linq maybe this code will help you:
List<DateTime> collectionOfDateTime = GetDateColletion();
collectionOfDateTime.GroupBy(s => Convert.ToInt16(s.DayOfWeek) & s.Hour & s.Minute);

Need to find last day of previous month and previous to previous month

I have to find the last date of last month(Nov) and a month before that(Oct).
I used the following code:
DateTime today = DateTime.Today;
DateTime endOfLastMonth = new DateTime(today.Year, today.Month - 1, 1).AddMonths(1).AddDays(-1);
DateTime endOfLastLastMonth = new DateTime(today.Year, today.Month - 2, 1).AddMonths(1).AddDays(-1);
The code works well for today but in the month of Jan the code fails because it supplies month part as 0 and -1.
Please let me know how to do that.
For the month of jan it should say 31 dec and 30 Nov.
The simplest way to find the last day of the previous month is to just find the first day of the current month (which will always be valid), and then subtract one day. Then do the same thing for the previous month.
DateTime startOfMonth = new DateTime(today.Year, today.Month, 1);
DateTime endOfPreviousMonth = startOfMonth.AddDays(-1);
DateTime endOfPreviousPreviousMonth = startOfMonth.AddMonths(-1).AddDays(-1);

Categories