Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Persian date format can take one of these formats: MM/YYYY or YYYY/MM
There is no day information, just year and month. I want to check a Persian date between two Persian dates: For example, 1400/01 is between 1399/07 and 1400/03 How can I do this while day information is not available?

What I have tried:

Please help me. I have no idea.
Posted
Updated 30-Mar-21 21:14pm
Comments
[no name] 30-Mar-21 13:58pm    
Use integers: year * 100 + month; then compare.
RedDk 30-Mar-21 17:58pm    
Whatever way works, presumeably ...
Maciej Los 31-Mar-21 2:36am    
Seems you're working on strings instead of dates...

1 solution

Take a look at below code:

C#
void Main()
{
	string one = "1399/07";
	string two = "03/1400";
	string three = "01/1400";
	DateTime dOne = GetDate(one);
	DateTime dTwo = GetDate(two);
	DateTime dThree = GetDate(three);
	bool answer = dThree>=dOne && dThree<=dTwo;
	Console.WriteLine($"'{dThree.ToString("yyyy-MM-dd")}' is between '{dOne.ToString("yyyy-MM-dd")}' and '{dTwo.ToString("yyyy-MM-dd")}'? => {answer}");
	
}

// Define other methods and classes here
public static DateTime GetDate(string initialValue)
{
	int pos = initialValue.IndexOf("/");
	string dformat = pos ==2 ? "MM/yyyy" : "yyyy/MM";
	DateTime dDate = DateTime.MinValue;
	DateTime.TryParseExact(initialValue, dformat, CultureInfo.GetCultureInfo("fa-IR"), DateTimeStyles.None, out dDate);
	return dDate;
}


For further details, please see:
DateTime.TryParseExact Method (System) | Microsoft Docs[^]
CultureInfo.GetCultureInfo Method (System.Globalization) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900