如何波斯日期转换成公历日期? [英] How to convert a Persian date into a Gregorian date?

查看:199
本文介绍了如何波斯日期转换成公历日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用了以下功能公历日期转换为波斯日期,但我已经无法写一个函数做反向转换。我想可以转换一个波斯日期到格鲁吉亚日期(如1390年7月18日12:00:00的字符串)的功能。

 公共静态字符串GetPdate(DateTime的_EnDate)
{
PersianCalendar pcalendar =新PersianCalendar();
串PDATE = pcalendar.GetYear(_EnDate)的ToString(0000)+/+
pcalendar.GetMonth(_EnDate)的ToString(00)+/+
pcalendar.GetDayOfMonth(_EnDate)的ToString(00)++
pcalendar.GetHour(_EnDate)的ToString(00)+:+
pcalendar.GetMinute(_EnDate)的ToString(00)+:+
pcalendar.GetSecond(_EnDate)的ToString(00);

返回PDATE;
}


解决方案

日期时间总是的公历,有效。即使你创建一个实例指定dfferent日历,由月返回的值,<$ C $ 。C>年度等方面的性能都在公历



作为一个例子,拿伊斯兰教历的启动:

 使用系统; 
使用System.Globalization;

类测试
{
静态无效的主要()
{
DateTime的划时代=新日期时间(1,1,1,新HijriCalendar()) ;
Console.WriteLine(epoch.Year); // 622
Console.WriteLine(epoch.Month); // 7
Console.WriteLine(epoch.Day); // 18
}
}



据你如何创建尚不清楚输入这个方法,或者是否应的真正的将其转换为字符串格式。 (或者你为什么不使用内置的字符串格式化)



它的可能的是,你可以使用:

 公共静态字符串FormatDateTimeAsGregorian(DateTime的输入)
{
返回input.ToString(YYYY'/'MM' /DDHH:毫米:SS,
CultureInfo.InvariantCulture);
}

这将为的<$ C $工作C>日期时间已创建的适当 - 但我们不知道你在这之前做了什么。



示例:

 ; 
使用System.Globalization;

类测试
{
静态无效的主要()
{
DateTime的划时代=新日期时间(1,1,1,新PersianCalendar()) ;
//打印0622/03/21零时00分00秒
Console.WriteLine(FormatDateTimeAsGregorian(纪元));
}

公共静态字符串FormatDateTimeAsGregorian(DateTime的输入)
{
返回input.ToString(YYYY'/'MM'/'DD'HH: 毫米:SS,
CultureInfo.InvariantCulture);
}
}

现在,如果你的指定日历当您创建的DateTime ,那么你就没有真正建立一个波斯日期都没有。



如果您希望保留他们的日历系统的轨道日期,你可以用我的野田佳彦时间的项目,该项目现在支持波斯语日历:

  //在Noda时间2.0你只是使用CalendarSystem.Persian 
变种persianDate =新LOCALDATE( 1390,7,18,CalendarSystem.GetPersianCalendar());
VAR gregorianDate = persianDate.WithCalendar(CalendarSystem.Iso);


I use the function below to convert Gregorian dates to Persian dates, but I've been unable to write a function to do the reverse conversion. I want a function that converts a Persian date (a string like "1390/07/18 12:00:00") into a Georgian date.

public static string GetPdate(DateTime _EnDate)
{
    PersianCalendar pcalendar = new PersianCalendar();
    string Pdate = pcalendar.GetYear(_EnDate).ToString("0000") + "/" +
       pcalendar.GetMonth(_EnDate).ToString("00") + "/" +
       pcalendar.GetDayOfMonth(_EnDate).ToString("00") + " " +
           pcalendar.GetHour(_EnDate).ToString("00") + ":" +
           pcalendar.GetMinute(_EnDate).ToString("00") + ":" +
           pcalendar.GetSecond(_EnDate).ToString("00");

    return Pdate;
}

解决方案

DateTime is always in the Gregorian calendar, effectively. Even if you create an instance specifying a dfferent calendar, the values returned by the Day, Month, Year etc properties are in the Gregorian calendar.

As an example, take the start of the Islamic calendar:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        DateTime epoch = new DateTime(1, 1, 1, new HijriCalendar());
        Console.WriteLine(epoch.Year);  // 622
        Console.WriteLine(epoch.Month); // 7
        Console.WriteLine(epoch.Day);   // 18
    }
}

It's not clear how you're creating the input to this method, or whether you should really be converting it to a string format. (Or why you're not using the built-in string formatters.)

It could be that you can just use:

public static string FormatDateTimeAsGregorian(DateTime input)
{
    return input.ToString("yyyy'/'MM'/'dd' 'HH':'mm':'ss",
                          CultureInfo.InvariantCulture);
}

That will work for any DateTime which has been created appropriately - but we don't know what you've done before this.

Sample:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        DateTime epoch = new DateTime(1, 1, 1, new PersianCalendar());
        // Prints 0622/03/21 00:00:00
        Console.WriteLine(FormatDateTimeAsGregorian(epoch));
    }

    public static string FormatDateTimeAsGregorian(DateTime input)
    {
        return input.ToString("yyyy'/'MM'/'dd' 'HH':'mm':'ss",
                              CultureInfo.InvariantCulture);
    }
}

Now if you're not specifying the calendar when you create the DateTime, then you're not really creating a Persian date at all.

If you want dates that keep track of their calendar system, you can use my Noda Time project, which now supports the Persian calendar:

// In Noda Time 2.0 you'd just use CalendarSystem.Persian
var persianDate = new LocalDate(1390, 7, 18, CalendarSystem.GetPersianCalendar());
var gregorianDate = persianDate.WithCalendar(CalendarSystem.Iso);

这篇关于如何波斯日期转换成公历日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆