问题描述
我的波斯日期格式可以采用以下格式之一:mm/YYYY 或 YYYY/mm 没有日期信息,只有年和月。 我想检查两个波斯日期之间的波斯日期: 例如,1400/01 介于 1399/07 和 1400/03 之间 当日信息不可用时,我该怎么做?
解决方法
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;
}
static void Main(string[] args)
{
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}");
Console.ReadKey();
}