将日期从波斯语转换为格里高利语

时间:2012-06-27 08:42:13

标签: c# .net datetime calendar

如何使用System.globalization.PersianCalendar将波斯日期转换为格里高利日期? 请注意,我想转换我的波斯日期(例如今天是1391/04/07)并获得格里高利日期结果,在这种情况下将是06/27/2012。 我正在数秒钟回答......

5 个答案:

答案 0 :(得分:65)

实际上非常简单:

// I'm assuming that 1391 is the year, 4 is the month and 7 is the day
DateTime dt = new DateTime(1391, 4, 7, persianCalendar);
// Now use DateTime, which is always in the Gregorian calendar

当您致电DateTime构造函数并传入Calendar时,它会为您转换 - 因此dt.Year将是2012年。如果您想采用其他方式,则需要构建相应的DateTime,然后使用Calendar.GetYear(DateTime)等。

简短但完整的计划:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        PersianCalendar pc = new PersianCalendar();
        DateTime dt = new DateTime(1391, 4, 7, pc);
        Console.WriteLine(dt.ToString(CultureInfo.InvariantCulture));
    }
}

打印06/27/2012 00:00:00。

答案 1 :(得分:9)

您可以使用此代码将波斯日期转换为格里高利。

// Persian Date
var value = "1396/11/27";
// Convert to Miladi
DateTime dt = DateTime.Parse(value, new CultureInfo("fa-IR"));
// Get Utc Date
var dt_utc = dt.ToUniversalTime();

答案 2 :(得分:2)

您可以使用此代码

return new DateTime(DT.Year,DT.Month,DT.Day,new System.Globolization.PersianCalendar());

答案 3 :(得分:0)

我有一个扩展方法:

export const postJobDescriptionQuickApply = (easyApplyData, jobId) => apiUrl('easyApply', 'easyApply').then(url => axios
  .post(url, {
    applicant: {
      email: easyApplyData.email,
      fullName: `${easyApplyData.firstName} ${easyApplyData.lastName}`,
      phoneNumber: easyApplyData.phoneNumber,
      resume: easyApplyData.resume,
      source: easyApplyData.source,
    },
    job: {
      jobId,
    },
  })
  .then((response) => {
    console.log('SUCCESS', response.data.developerMessage)
    return response.data.developerMessage
  })
  .catch((error) => {
    // handle error
    console.log('ERROR JOB DESCRIPTION', error.response.data.developerMessage)
    return error.response.data.developerMessage
  })

示例:将 public static DateTime PersianDateStringToDateTime(this string persianDate) { PersianCalendar pc = new PersianCalendar(); var persianDateSplitedParts = persianDate.Split('/'); DateTime dateTime = new DateTime(int.Parse(persianDateSplitedParts[0]), int.Parse(persianDateSplitedParts[1]), int.Parse(persianDateSplitedParts[2]), pc); return DateTime.Parse(dateTime.ToString(CultureInfo.InvariantCulture)); } 转换为1398/07/05

答案 4 :(得分:0)

我在Windows 7和10上测试了此代码,并且运行良好,没有任何问题

    /// <summary>
    /// Converts a Shamsi Date To Milady Date
    /// </summary>
    /// <param name="shamsiDate">string value in format "yyyy/mm/dd" or "yyyy-mm-dd"                     
     ///as shamsi date </param>
    /// <returns>return a DateTime in standard date format </returns>
public static DateTime? ShamsiToMilady(string shamsiDate)
    {
        if (string.IsNullOrEmpty(shamsiDate))
            return null;

        string[] datepart = shamsiDate.Split(new char[] { '/', '-' });
        if (datepart.Length != 3)
            return null;
        // 139p/k/b validation
        int year = 0, month = 0, day = 0;
        DateTime miladyDate;
        try
        {
            year = datepart[0].Length == 4 ? int.Parse(datepart[0]) : 
                                    int.Parse(datepart[2]);
            month = int.Parse(datepart[1]);
            day = datepart[0].Length == 4 ? int.Parse(datepart[2]) : 
                               int.Parse(datepart[0]);
            var currentCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            miladyDate = new DateTime(year, month, day, new PersianCalendar());
            Thread.CurrentThread.CurrentCulture = currentCulture;
        }
        catch
        {
            return null;
        }

        return miladyDate;
    }
相关问题