Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Persian Calendar date string to DateTime?

Tags:

c#

datetime

I use these codes for converting current datetime and minus it with the date time which users type in a textbox. But it gives an error while converting.

PersianCalendar p = new System.Globalization.PersianCalendar();
DateTime date = new DateTime();
date = DateTime.Parse(DateTime.Now.ToShortDateString());
int year = p.GetYear(date);
int month = p.GetMonth(date);
int day = p.GetDayOfMonth(date);
string str = string.Format("{0}/{1}/{2}", year, month, day);
DateTime d1 = DateTime.Parse(str);
DateTime d2 = DateTime.Parse(textBox9.Text);
string s = (d2 - d1).ToString().Replace(".00:00:00", "");
textBox10.Text = (d2 - d1).ToString().Replace(".00:00:00","");

This line will accord an error while converting datetime from string to date time :DateTime d1 = DateTime.Parse(str);

Please help me to solve this problem.

Thanks in advance

like image 881
Ali Vojdanian Avatar asked May 18 '12 15:05

Ali Vojdanian


2 Answers

No need to parse the date here.

EDIT:

Originally I only wanted to point out error in OP processing with this answer. But I think a full answer would be better (despite the delay :) ) And Yes I know that intermediate date representation does not look like Persian Date. But it is not what is needed. This code gives proper difference between current date and the date that user puts in.

string userInput = "1394/02/31";
System.DateTime date = System.DateTime.Today;
System.Globalization.PersianCalendar p = new System.Globalization.PersianCalendar();

int year = p.GetYear(date);
int month = p.GetMonth(date);
int day = p.GetDayOfMonth(date);
System.DateTime currentDate = new System.DateTime(year, month, 1);
currentDate = currentDate.AddDays(day - 1);

// validation omitted
System.String[] userDateParts = userInput.Split(new[] { "/" }, System.StringSplitOptions.None);
int userYear = int.Parse(userDateParts[0]);
int userMonth = int.Parse(userDateParts[1]);
int userDay = int.Parse(userDateParts[2]);
System.DateTime userDate = new System.DateTime(userYear, userMonth, 1);
userDate = userDate.AddDays(userDay - 1);

System.TimeSpan difference = currentDate.Subtract(userDate);
like image 118
Grzegorz W Avatar answered Oct 12 '22 23:10

Grzegorz W


You actual problem seems to lie in parsing the date the user entered in Persian calendar format. So, if the user entered 1391/2/31 you want to be able to parse that. Today (May 19th 2012) is 1391/2/30 so you finally want to display something like 1 day remaining.

This question has been asked before. However, the accepted answer there just tries to use

string persianDate = "1390/02/07";
CultureInfo persianCulture = new CultureInfo("fa-IR");
DateTime persianDateTime = DateTime.ParseExact(persianDate, 
    "yyyy/MM/dd", persianCulture);

Which won't work for a date like 1391/2/31. So I think you'd have to implement the parsing yourself.

Without implementing any error checking, and assuming the user always uses the format yyyy/mm/dd you could use:

// Convert Persian Calendar date to regular DateTime
var persianDateMatch = System.Text.RegularExpressions.Regex.Match(
    persianTargetTextBox.Text, @"^(\d+)/(\d+)/(\d+)$");
var year = int.Parse(persianDateMatch.Groups[1].Value);
var month = int.Parse(persianDateMatch.Groups[2].Value);
var day = int.Parse(persianDateMatch.Groups[3].Value);
var pc = new System.Globalization.PersianCalendar();
var target = pc.ToDateTime(year, month, day, 0, 0, 0, 0);

var difference = target - DateTime.Today;
differenceLabel.Text = difference.TotalDays.ToString("0");

You would need to check if the regex acually found any matches. You'd also have to check if pc.ToDateTime doesn't throw an error. Unfortunately, there doesn't seem to exist anything like DateTime.TryParse for the Persian Calendar.

Anyway, you don't need to parse today into a Persian Calendar, you only need to parse the user input.

You might also be interested in Farsi Library - Working with Dates, Calendars, and DatePickers, even though the article is very old (2007).

like image 39
comecme Avatar answered Oct 12 '22 23:10

comecme