ExtensionMethod.NET Home of 820 C#, Visual Basic, F# and Javascript extension methods

Persian DateTime

Convert DateTime To PersianDate

Source

public static string ToPersianDate(this DateTime? dt)
{
    try
    {
        var dateTime = dt.ToDateTime();
        PersianCalendar persianCalendar = new PersianCalendar();
        string year = persianCalendar.GetYear(dateTime).ToString();
        string month = persianCalendar.GetMonth(dateTime).ToString()
                       .PadLeft(2, '0');
        string day = persianCalendar.GetDayOfMonth(dateTime).ToString()
                       .PadLeft(2, '0');
        string hour = dateTime.Hour.ToString().PadLeft(2, '0');
        string minute = dateTime.Minute.ToString().PadLeft(2, '0');
        string second = dateTime.Second.ToString().PadLeft(2, '0');
        return String.Format("{0}/{1}/{2} {3}:{4}:{5}", year, month, day, hour, minute, second);

    }
    catch
    {

        throw;
    }
}

Example

var persianDate=DateTime.Now().ToPersionDate();

Author: http://www.kaperco.com

Submitted on: 19 jan 2016

Language: C#

Type: PersianCalendar

Views: 2435