Hebrew/Jewish date conversion
I would like to convert Gregorian dates (the ones that we usually use on America) to Hebrew (Jewish) dates. I'm using Adobe Flash CS5 for an Adobe AIR application. Is there some code for this already developed on ActionScript 3.0?
After some time researching I discovered that windows applications created on Visual Studio have built-in f开发者_StackOverflowunctions for such need. Is there some way of calling a function on a windows form or something from AIR?
Unfortunately after searching around, I cannot find any library or anything to get hebrew date in flash. However, I have found this site:
http://www.hebcal.com/converter/?gd=21&gm=6&gy=2011&g2h=Convert+Gregorian+to+Hebrew+date
It appears that hebcal.com accepts the gregorian date in GET variables within the URL, and returns the correct Hebrew date. So, you could in theory use the flash URLLoader to load a URL composed of your current gregorian date inside of flash, and then parse the HTML data in the request response to extract the hebrew date.
I went ahead and figured out how to do this for you using the web service, hebcal.com. Here is the actionscript 3 code to accomplish this task:
import flash.net.URLLoader;
import flash.events.Event;
import flash.net.URLRequest;
var currentDate:Date = new Date();
var dateLoader:URLLoader = new URLLoader();
var dateQueryURL:String = 'http://www.hebcal.com/converter/?gd=' + currentDate.date + '&gm=' + (currentDate.month+1) + '&gy=' + currentDate.fullYear + '&g2h=Convert+Gregorian+to+Hebrew+date';
trace(dateQueryURL);
dateLoader.addEventListener(Event.COMPLETE, onDateConverted);
dateLoader.load(new URLRequest(dateQueryURL));
var hebrewDateSearchRegexp:RegExp = /([a-zA-Z0-9 ,]{1,30})(?=<\/b)/gixm;
function onDateConverted(e:Event):void
{
var queryResult:String = e.currentTarget.data as String;
var hebrewDate:String;
var dateArr:Array = queryResult.match(hebrewDateSearchRegexp);
if(dateArr && dateArr.length > 0){
//The date was found in the string
hebrewDate = dateArr[0];
trace("The current Hebrew date is: " + hebrewDate);
}else{
trace("Error retrieving current Hebrew date.");
}
}
So basically I just use a regular expression that finds/selects only the part of the HTML that contains the Hebrew date. I then extract this using string.match() and output it in a trace statement.
Also note that with the currentDate.month, I had to increment this by 1, because flash counts the months from 0-11, whereas the tool on hebcal.com counts from 1-12.
I'm not sure about adobe products, but since your question is taged with C# (which looks weird to me), I'll provide a C# ex. method that does it very efficiently:
/// <summary>
/// Converts a gregorian date to its hebrew date string representation,
/// using custom DateTime format string.
/// </summary>
/// <param name="value">The <see cref="DateTime"/> value to convert.</param>
/// <param name="format">A standard or custom date-time format string.</param>
public static string ToJewishDateString(this DateTime value, string format)
{
var ci = CultureInfo.CreateSpecificCulture("he-IL");
ci.DateTimeFormat.Calendar = new HebrewCalendar();
return value.ToString(format, ci);
}
精彩评论