Home > Back-end >  How to determine upcoming months relative to a given month in any calendar?
How to determine upcoming months relative to a given month in any calendar?

Time:12-27

How can I find out the upcoming months relative to a given month according to the calendar that I select?

Meaning I selected the Gregorian calendar and the given month is September, a list of the following months will appear:

[September, October, November, December]

If the Hijri calendar is selected and the current month is: Dhu al-Qa’dah, a list of the remaining months will appear, which are:

[Dhul Qi'dah, Dhul Hijjah]

I used the following reference to make the code but it didn't work

System.Globalization.CultureInfo(DateLangCulture, false).DateTimeFormat

CodePudding user response:

In .NET Framework, month names are based on a culture. If I understand your question clearly, you wanna select some "Calendar" and wanna list some months of it. I think there is a problem with this approach because different cultures might use the same calendar and these different cultures might have different month names.

For example, let's take enter image description here

You can get the current month number as;

var currentMonthNo = Datetime.Now.Month;

then you can start to iterate from it's previous index (since array start index 0) to the end of the array (Be aware, there are some calendars that have 13 months, that's why for 12 month cultures the 13th element of the array is an empty string.)

CodePudding user response:

Only supplement Soner Gönül's answer.

If you want to get the Gregorian calendar month according to the current system calendar, you can refer to the following code:

namespace ConsoleApp1 {
    internal class Program {
        static void Main (string[] args) {
            //CultureInfo culture = CultureInfo.GetCultureInfo("en-US");
            //CultureInfo culture = CultureInfo.GetCultureInfo("ar-SA");
            CultureInfo culture = CultureInfo.GetCultureInfo("fr");
            List<KeyValuePair<int,string>> months = new List<KeyValuePair<int,string>>();
            for (int i = getCurMonth(); i <= 12; i  )
                {
                months.Add(new KeyValuePair<int, string>(i, DateTime.Parse(DateTime.Now.Year.ToString()   "-"   i   "-1").ToString("MMMM", culture)));
                }
            foreach (KeyValuePair<int, string> month in months)
                {
                Console.WriteLine(month.Value);
                }
            Console.ReadLine();
            }
        static int getCurMonth () {
            return DateTime.Now.Month;
            }
        }
    }

To obtain a specific country calendar, the system must install the corresponding character set.

You can see the following example:

en-us

CultureInfo culture = CultureInfo.GetCultureInfo("en-US");

Output:

enter image description here

fr

CultureInfo culture = CultureInfo.GetCultureInfo("fr")

Output:

enter image description here

If you have questions about my code, please comment below.

  • Related