How I can to Convert Shamsi date to Miladi date ? For example my value is 1374/06/27 and I want to convert this date to Miladi date 1995/09/18. I can't find function for convert Shamsi to Miladi . What is this function?
This answer assumes that "Shamsi" means "Solar Hijri" or "Persian" calendar, and "Miladi" means "Gregorian Calendar". That's what my research suggests, though I haven't heard either term before.
If you're happy using the built-in types, you can perform the conversion quite simply like this:
Calendar persian = new PersianCalendar();
DateTime date = new DateTime(1374, 6, 27, persian);
Console.WriteLine(date.Year); // 1995
Console.WriteLine(date.Month); // 9
Console.WriteLine(date.Day); // 18
Basically DateTime is always in the Gregorian calendar system, even if you construct a value using a different calendar system. (Note that just using Console.WriteLine(date) will convert it back into the default calendar system of your default culture, which is why I didn't do that in the same code.)
In the reverse direction, you'd need to use persian.GetYear(date) etc.
Personally I'm not a big fan of System.DateTime etc - and in my NodaTime library, when you specify a calendar system, that's part of the value itself. So you can construct a date with the Persian calendar system, and that's the calendar system it will use forever - but it's easy to convert it to a date in the Gregorian calendar system, and vice versa:
LocalDate persianDate = new LocalDate(1374, 6, 27, CalendarSystem.PersianArithmetic);
LocalDate gregorianDate = persianDate.WithCalendar(CalendarSystem.Gregorian);
Obviously I think it's worth using NodaTime for this clarity - and many other reasons - but the choice is yours.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With