我使用下面的函数将格里高利日期转换为波斯日期,但我一直无法编写函数来进行反向转换.我想要一个将波斯日期(像"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;
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 13
DateTime是总是在公历,有效.即使你创建一个实例指定dfferent日历,通过返回的值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
}
}
Run Code Online (Sandbox Code Playgroud)
目前尚不清楚你如何创建输入此方法,或者你是否应该真正地将其转换为字符串格式.(或者为什么你没有使用内置的字符串格式化程序.)
这可能是你可以使用:
public static string FormatDateTimeAsGregorian(DateTime input)
{
return input.ToString("yyyy'/'MM'/'dd' 'HH':'mm':'ss",
CultureInfo.InvariantCulture);
}
Run Code Online (Sandbox Code Playgroud)
这将适用于任何 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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果您在创建日历时未指定日历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);
Run Code Online (Sandbox Code Playgroud)
谢谢大家,我使用下面的代码,我的问题解决了:
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;
}
Run Code Online (Sandbox Code Playgroud)
使用此代码可以将波斯日期转换为公历日期,反之亦然。
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;
}
}
Run Code Online (Sandbox Code Playgroud)
以这种方式使用代码
var time = DateTime.Now;
var persianDate = time.ConverToPersian();
var gregorianDate = persianDate.ConverToGregorian();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15226 次 |
| 最近记录: |