在C#中将dateTime类型转换为HebrewCalender [英] Convert dateTime type to HebrewCalender in c#
问题描述
大家好!
我在c#中有此代码:
Hi Everyone!
I have this code in c#:
public static string GetHebrewJewishDateString(DateTime anyDate)
{
System.Text.StringBuilder hebrewFormatedString = new System.Text.StringBuilder();
// Create the hebrew culture to use hebrew (Jewish) calendar
CultureInfo jewishCulture = CultureInfo.CreateSpecificCulture("he-IL");
jewishCulture.DateTimeFormat.Calendar = new HebrewCalendar();
// Day of the week in the format " "
hebrewFormatedString.Append(anyDate.ToString("dddd", jewishCulture) + " ");
// Day of the month in the format "'"
hebrewFormatedString.Append(anyDate.ToString("dd", jewishCulture) + " ");
// Month and year in the format " "
hebrewFormatedString.Append("" + anyDate.ToString("y", jewishCulture));
return hebrewFormatedString.ToString();
}
该函数获取一个公历的DateTime并以字符串形式返回相等的希伯来语Date.
我想做相反的意思是:向函数发送一个希伯来日期字符串,并获得相等的公历日期时间.
确实,我将要检查某个特定的希伯来语日期是否介于其他两个希伯来语日期之间,因此,如果有人有更有效的主意(而不是将整个三个日期都转换为DateTime),我将非常高兴听到.
非常感谢!!
塔利.
the function get a gregorian DateTime and returns the equal Hebrew Date as a string.
I would like to do the opposite, means: to send a function a string of hebrew date and to get the equal Gregorian DateTime.
truly, I''m about to check if a specific Hebrew Date falls between two other Hebrew dates, so if someone have more effective idea (instead of converting the whole three dates to DateTime) - I''d be very glad to hear.
Thanks a lot !!
Tali.
推荐答案
好,我看到您正在转换字符串.我认为您的问题不需要这样做.无论如何,这是一个解决方案.
DateTime.Parse和ToString方法可提供所需的一切.
看看这个示例应用程序:
Ok, I see you converting a string. In my opinion this is not needed for your problem. Anyway here is a solution.
The DateTime.Parse and ToString methods provide everything you need.
Look at this example app:
using System;
using System.Globalization;
namespace DateTimeConverter
{
class Program
{
static CultureInfo cultureJewish = CultureInfo.CreateSpecificCulture("he-IL");
static void Main(string[] args)
{
cultureJewish.DateTimeFormat.Calendar = new HebrewCalendar();
// create a hebrew date
DateTime dtAnyDateHebrew = new DateTime(5767, 1, 1, new System.Globalization.HebrewCalendar());
// write as hebrew date
Console.WriteLine(dtAnyDateHebrew.ToString(cultureJewish));
// write as current culture date
Console.WriteLine(dtAnyDateHebrew.ToString());
// convert date from hebrew datetime string (this is not realy needed, but you asked for it)
DateTime dtConverted = ParseHebrewDateTimeString(dtAnyDateHebrew.ToString(cultureJewish));
Console.WriteLine(dtConverted);
// BTW: You can compare DateTimes like integers
DateTime dtHebrewBegin = new DateTime(5767, 1, 1, new System.Globalization.HebrewCalendar());
DateTime dtHebrewMiddle = new DateTime(5768, 1, 1, new System.Globalization.HebrewCalendar());
DateTime dtHebrewEnd = new DateTime(5769, 1, 1, new System.Globalization.HebrewCalendar());
// ...
if (dtHebrewMiddle > dtHebrewBegin && dtHebrewMiddle < dtHebrewEnd)
Console.WriteLine("Date {0} falls between {1} and {2}",
dtHebrewMiddle.ToString(cultureJewish),
dtHebrewBegin.ToString(cultureJewish),
dtHebrewEnd.ToString(cultureJewish));
Console.ReadKey();
}
public static DateTime ParseHebrewDateTimeString(string strHebrew)
{
DateTime dtResult = DateTime.Parse(strHebrew, cultureJewish);
return dtResult;
}
}
}
非常有用的代码.
非常感谢.
Very useful codes.
Thanks a lot.
这篇关于在C#中将dateTime类型转换为HebrewCalender的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!