Home > front end >  Asp.Net Core : Calculate two Persian dates and get the age?
Asp.Net Core : Calculate two Persian dates and get the age?

Time:03-09

I have two Persian dates and I want the first date to be subtracted from the second date and to store the number as an age in a variable.

First Date : 1399/01/01 Second Date : 1400/01/01 I want the date to be calculated as follows?

Calcute : 1399 - 1400 = 1

CodePudding user response:

I found the answers to my questions

  PersianCalendar pc = new PersianCalendar();
            var Birthdate =  pc.GetYear(DateTime.Parse(viewmodel.BirthDateDay));
            var DateNow = pc.GetYear(DateTime.Now);                
            var resault = ((Convert.ToInt32(DateNow) - Convert.ToInt32(Birthdate))   " "   "سال");
 

CodePudding user response:

You can use PersianCalendar in .NET like code below then minus date2 from date1 which gives you a TimeSpan object (time between two dates).

        PersianCalendar pc = new PersianCalendar();
        DateTime dt1 = new DateTime(1399, 01, 01, pc);
        DateTime dt2 = new DateTime(1400, 01, 01, pc);
        TimeSpan timeBetween = dt2 - dt1;
        Console.WriteLine(timeBetween);
  • Related