4

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?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Soheila Tarighi
  • 487
  • 4
  • 15
  • 1
    I believe the Shamsi calendar is what I'd normally know as the Persian or Solar Hijri calendar - but I haven't heard of "Miladi" before, and I'm struggling to find any references. For the example you've given, it would work if "Miladi" is the Gregorian calendar system, but I don't want to assume that. – Jon Skeet Jun 29 '20 at 16:46
  • Same question for JavaScript is [here](/q/43850436) – cachius May 07 '22 at 14:21

2 Answers2

8

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.

Using System.DateTime

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.

Using NodaTime

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    thank you for your answer. how to i change this result to 1995/09/18 – Soheila Tarighi Jun 29 '20 at 18:02
  • 1
    @SoheilaTarighi: That's a matter of formatting a date - there are *lots* of questions about that on Stack Overflow already. I'd strongly recommend specifying the invariant culture when you format, to make sure it doesn't convert it back to a different calendar system. – Jon Skeet Jun 29 '20 at 19:42
0

Persia .NET Core is a class library to convert Persian, Gregorian, and Arabic (Hijri) dates to each other. This library has been developed based on .NET Core 3.1

Converting Gregorian to Shamsi

Persia.SolarDate solarDate = Persia.Calendar.ConvertToPersian(DateTime.Now);
// getting the simple format of persian date
string str = solarDate.ToString();

Converting Shamsi to Gregorian

DateTime ConvertToGregorian(SolarDate solarDate)
DateTime ConvertToGregorian(LunarDate lunarDate)
DateTime ConvertToGregorian(int year, int month, int day, DateType dateType)
DateTime ConvertToGregorian(int year, int month, int day, int hour, int minute, int
second, DateType dateType)

more details and download from here

Majid Shahabfar
  • 4,010
  • 2
  • 28
  • 36