【问题标题】:Convert Date from Persian to Gregorian将日期从波斯语转换为公历
【发布时间】:2012-06-27 08:42:13
【问题描述】:

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

【问题讨论】:

标签: c# .net datetime calendar


【解决方案1】:

其实很简单:

// 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 太棒了! !不知道DateTime 中的Calender 条款,认为GetYearGetMonth 是可行的方法
  • ToDateTime 类的ToDateTime 方法呢??
  • 我们如何使用dash?意思是“201-06-27 00:00:00”
  • @Saeed:只需在 ToString 调用中指定自定义格式即可。
  • @Jon: DateTime dateEC = new DateTime(datePersian.Year,datePersian.Month,datePersian.Day, new PersianCalendar()); 我使用了这个代码。
【解决方案2】:

您可以使用此代码将波斯日期转换为公历。

// 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();

【讨论】:

  • 如此简单实用...谢谢
  • CultureInfo("fa-IR") 的默认日历在 Windows 7 中不是“波斯日历”。因此请确保此代码在 Windows 7 中正常工作(如有必要)
  • 不适用于服务器主机,因为默认日历不是波斯日历
【解决方案3】:

你可以使用这个代码

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

【讨论】:

  • 欢迎来到 Stack Overflow。仅代码答案几乎总是可以通过添加一些关于它们的工作方式和原因的解释来改进。在用现有答案回答较旧的问题时,指出您的答案所针对的问题的新方面非常重要。如果问题很老,那么说明您使用的技术是在提出问题后的某个时间添加的,或者它们是否依赖于特定版本的语言或程序,这可能会很有用。
  • 另外,据我所知,虽然您以不同的方式拉动变量,但这本质上与八年前公认的答案相同 - 但解释和拼写要少得多错误。在用已接受的答案回答旧问题时,请考虑您的答案是否添加了任何新内容——如果是,请向读者解释为什么您的表述可能比现有贡献更可取。你介意更新你的答案吗?
【解决方案4】:

我有一个扩展方法:

 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.CreateSpecificCulture("en-US")));
 }

For more formats and culture-specific formats

示例:将1391/04/07 转换为06/27/2012

【讨论】:

    【解决方案5】:

    我在 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;
        }
    

    【讨论】:

    • 此代码既不补充/增强先前的答案,也不使用问题所指的System.globalization.PersianCalendar。请参考stackoverflow.com/a/62176697/6045793上的cmets,了解更多关于如何回答老话题的信息。
    • 感谢您对我的关注,但我使用 System.globalization.PersianCalendar 行 miladyDate = new DateTime(year, month, day, new PersianCalendar());我的代码适用于所有条件并赢得操作系统。请再看stackoverflow.com/users/6045793/mahdad-baghani
    • 是的,你是对的。我一定错过了您使用 System.globalization.PersianCalender 的部分。但是,您的答案仍然缺乏对以前答案的增强。不要把这当作一种挫败感。我认为通过应用更好的命名(Shamsi -> Persian, Milady -> Gregorian),您的答案可以得到支持。此外,您似乎正在使用自己的约定将字符串日期解析为年、月和日变量,这可能并非在所有情况下都有效。
    猜你喜欢
    • 2023-03-30
    • 2019-07-17
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 2021-04-29
    • 2018-07-29
    • 1970-01-01
    相关资源
    最近更新 更多