我使用下面的函数将格里高利日期转换为波斯日期,但我一直无法编写函数来进行反向转换。我想要一个将波斯日期(像“1390/07/18 12:00:00”这样的字符串)转换为格鲁吉亚日期的函数。
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;
}
答案 0 :(得分:13)
DateTime
在格里高利历中总是 。即使您创建了指定不同日历的实例,Day
,Month
,Year
等属性返回的值也会在格里高利历中。
举个例子,开始伊斯兰历法:
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
}
}
目前尚不清楚如何创建此方法的输入,或者是否将其转换为字符串格式。 (或者为什么你没有使用内置的字符串格式化程序。)
可以是你可以使用:
public static string FormatDateTimeAsGregorian(DateTime input)
{
return input.ToString("yyyy'/'MM'/'dd' 'HH':'mm':'ss",
CultureInfo.InvariantCulture);
}
这适用于已经正确的任何 DateTime
- 但我们在此之前不知道你做了什么。
样品:
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);
}
}
现在如果您在创建DateTime
时不指定日历,那么您根本就不会创建波斯日期。
如果您想要跟踪日历系统的日期,可以使用我现在支持波斯日历的Noda Time项目:
// 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);
答案 1 :(得分:8)
谢谢大家,我使用下面的代码解决了我的问题:
public static DateTime GetEdate(string _Fdate)
{
DateTime fdate = Convert.ToDateTime(_Fdate);
GregorianCalendar gcalendar = new GregorianCalendar();
DateTime eDate = pcalendar.ToDateTime(
gcalendar.GetYear(fdate),
gcalendar.GetMonth(fdate),
gcalendar.GetDayOfMonth(fdate),
gcalendar.GetHour(fdate),
gcalendar.GetMinute(fdate),
gcalendar.GetSecond(fdate), 0);
return eDate;
}
答案 2 :(得分:4)
1-首次安装Persiandate NuGet package(17KB)
Install-Package PersianDate
2-使用ToFa和toEn PersianDate类的静态方法(不要担心库本身将处理的分隔符和方向,如下面的示例所示。也可以在里面找到Usage Console项目GitHub可以自己轻松测试一下):
//default format
string dts=ConvertDate.ToFa(DateTime.Now);//1393/08/01
//date only (short and D for Long)
dts=ConvertDate.ToFa(DateTime.Now, "d");//93/08/01
dts=ConvertDate.ToFa(DateTime.Now, "D");//پنج شنبه, 01 آبان 1393
//time only
dts=ConvertDate.ToFa(DateTime.Now, "t");//21:53
dts=ConvertDate.ToFa(DateTime.Now, "T");//21:53:26
//general short date + time
dts=ConvertDate.ToFa(DateTime.Now, "g");//93/08/01 21:53
dts=ConvertDate.ToFa(DateTime.Now, "G");//93/08/01 21:53:26
//general full date + time
dts=ConvertDate.ToFa(DateTime.Now, "f");//پنج شنبه, 01 آبان 1393 21:53
dts=ConvertDate.ToFa(DateTime.Now, "F");//پنج شنبه, 01 آبان 1393 21:53:26
//only month and year
dts=ConvertDate.ToFa(DateTime.Now, "m");//آبان 1
dts=ConvertDate.ToFa(DateTime.Now, "y");//1393 آبان
dts=ConvertDate.ToFa(DateTime.Now);//1393/08/01
// converting back to Gregorian date
Datetime dt= ConvertDate.ToEn(dts);//2014/10/23 00:00:00
3- Source code available in GitHub 用于任何修改和支持新格式。
查找以下函数源代码,了解它是如何工作的
public static DateTime ToEn(string fadate)
{
if (fadate.Trim() == "") return DateTime.MinValue;
int[] farsiPartArray = SplitRoozMahSalNew(fadate);
return new PersianCalendar().ToDateTime(farsiPartArray[0], farsiPartArray[1], farsiPartArray[2], 0, 0, 0, 0);
}
private static int[] SplitRoozMahSalNew(string farsiDate)
{
int pYear = 0;
int pMonth = 0;
int pDay = 0;
//normalize with one character
farsiDate = farsiDate.Trim().Replace(@"\", "/").Replace(@"-", "/").Replace(@"_", "/").
Replace(@",", "/").Replace(@".", "/").Replace(@" ", "/");
string[] rawValues = farsiDate.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (!farsiDate.Contains("/"))
{
if (rawValues.Length != 2)
throw new Exception("usually there should be 2 seperator for a complete date");
}
else //mostly given in all numeric format like 13930316
{
// detect year side and add slashes in right places and continue
}
//new simple method which emcompass below methods too
try
{
pYear = int.Parse(rawValues[0].TrimStart(new[] { '0' }).Trim());
pMonth = int.Parse(rawValues[1].TrimStart(new[] { '0' }).Trim());
pDay = int.Parse(rawValues[2].TrimStart(new[] { '0' }).Trim());
// the year usually must be larger than 90
//or for historic values rarely lower than 33 if 2 digit is given
if (pYear < 33 && pYear > 0)
{
//swap year and day
pYear = pDay;
pDay = int.Parse(rawValues[0]); //convert again
}
//fix 2 digits of persian strings
if (pYear.ToString(CultureInfo.InvariantCulture).Length == 2)
pYear = pYear + 1300;
//
if (pMonth <= 0 || pMonth >= 13)
throw new Exception("mahe shamsi must be under 12 ");
}
catch (Exception ex)
{
throw new Exception(
"invalid Persian date format: maybe all 3 numric Sal, Mah,rooz parts are not present. \r\n" + ex);
}
return new[] { pYear, pMonth, pDay };
}
答案 3 :(得分:4)
使用此代码将波斯语转换为格里高利日期,反之亦然。
public static class PersainDate
{
public static DateTime ConvertToGregorian(this DateTime obj)
{
DateTime dt = new DateTime(obj.Year, obj.Month, obj.Day, new PersianCalendar());
return dt;
}
public static DateTime ConvertToPersian(this DateTime obj)
{
var persian = new PersianCalendar();
var year = persian.GetYear(obj);
var month = persian.GetMonth(obj);
var day = persian.GetDayOfMonth(obj);
DateTime persiandate = new DateTime(year, month, day);
return persianDate;
}
}
以这种方式使用代码
var time = DateTime.Now;
var persianDate = time.ConverToPersian();
var gregorianDate = persianDate.ConverToGregorian();
答案 4 :(得分:2)
You may try with this:
using System.Globalization // namespace
GregorianCalendar greg = new GregorianCalendar();
DateTime DayYearMonth = new DateTime(1433, 10, 11, greg );
Console.WriteLine(greg .GetDayOfMonth(DayYearMonth )); // 11
Console.WriteLine(greg .GetYear(DayYearMonth )); // 1433
Console.WriteLine(greg .GetMonth(DayYearMonth )); // 10
答案 5 :(得分:1)
此方法将表示shamsi日期的字符串转换为System.DateTime:
public DateTime milady(string shamsiDate)
{
DateTime dt = new DateTime();
PersianCalendar pc = new PersianCalendar();
int pYear = Convert.ToInt32(shamsiDate.Substring(0, 4));
int pMonth =Convert.ToInt32(shamsiDate.Substring(5, 2));
int pDay = Convert.ToInt32( shamsiDate.Substring(8));
dt = pc.ToDateTime(pYear, pMonth, pDay,0,0,0,0);
return dt;
}
答案 6 :(得分:1)
这种方法检查闰年。
private static DateTime JalaliToGregorianDate(string jalaliDate)
{
string[] JalaliDateArray = jalaliDate.Split(new Char[] { '/' });
int Year, Mounth, Day;
DateTime GregorianDate;
PersianCalendar persianCalendar = new PersianCalendar();
//
Year = int.Parse(JalaliDateArray[2]);
if (Year < 100)
{
Year = 1300 + Year;
}
Mounth = int.Parse(JalaliDateArray[1]);
Day = int.Parse(JalaliDateArray[0]);
if (Day > 29 && Mounth == 12)
{
if (!persianCalendar.IsLeapYear(Year))
{
throw (new Exception(string.Format("IsNotLeapYear&{0}", Year)));
}
}
GregorianDate = persianCalendar.ToDateTime(Year, Mounth, Day, 0, 0, 0, 0);
//
return GregorianDate;
}
答案 7 :(得分:1)
使用Persia.NET Core可以为您提供帮助:
将格里高里格语转换为Shamsi
Persia.SolarDate solarDate = Persia.Calendar.ConvertToPersian(DateTime.Now);
// getting the simple format of persian date
string str = solarDate.ToString();
将Shamsi转换为公历
DateTime ConvertToGregorian(SolarDate solarDate)
DateTime ConvertToGregorian(LunarDate lunarDate)
DateTime ConvertToGregorian(int year, int month, int day, DateType dateType)
DateTime ConvertToGregorian(int year, int month, int day, int hour, int minute, int
second, DateType dateType)
您可以here下载它 如果您的应用程序不支持.NET Core,则可以从NuGet中找到旧版本。
答案 8 :(得分:0)
你可以使用Nuget:
来使用PersianDateTimePM> Install-Package PersianDateTime
以及如何使用:
DateTime miladiDate = new DateTime(2013, 5, 31);
PersianDateTime persianDate = new PersianDateTime(miladiDate);
和:
PersianDateTime persianDate = PersianDateTime.Parse("1392/03/02");
DateTime miladiDate = persianDate.ToDateTime();