如何用C#获取耶路撒冷当前时间?

时间:2012-10-05 13:06:54

标签: c# timezone

我需要知道耶路撒冷当前时间。

该代码需要服务器时间,但我需要耶路撒冷时间:

DateTime currentTime = DateTime.Now;
dayName = currentTime.DayOfWeek;

编辑:

在Vinoth回答的帮助下(我只使用了AddHours(2)部分)它应该是那样的(不起作用):

      DateTime currentTime = DateTime.Now;
      currentTime=currentTime.AddHours(2);//Jerusalem Time
      dayName = currentTime.DayOfWeek;

Edit2:我的改进(ToUniversalTime())

      DateTime currentTime = DateTime.Now;
      currentTime=currentTime.ToUniversalTime().AddHours(2);//Jerusalem Time
      dayName = currentTime.DayOfWeek;

2 个答案:

答案 0 :(得分:4)

这会对你有所帮助。我用过这是我的应用程序之一。 Jus粘贴代码

public static DateTime GetIsraelTime(DateTime d) {
    d = d.AddHours(2);           // Israel is at GMT+2

    // April 2nd, 2:00 AM
    DateTime DSTStart = new DateTime(d.Year, 4, 2, 2, 0 ,0);  
    while (DSTStart.DayOfWeek != DayOfWeek.Friday)
        DSTStart = DSTStart.AddDays(-1);

    CultureInfo jewishCulture = CultureInfo.CreateSpecificCulture("he-IL");
    System.Globalization.HebrewCalendar cal = 
          new System.Globalization.HebrewCalendar();
    jewishCulture.DateTimeFormat.Calendar = cal;
    // Yom HaKipurim, at the start of the next Jewish year, 2:00 AM
    DateTime DSTFinish =
         new DateTime(cal.GetYear(DSTStart)+1, 1, 10, 2, 0 ,0, cal);
    while (DSTFinish.DayOfWeek != DayOfWeek.Sunday)
        DSTFinish= DSTFinish.AddDays(-1);

    if (d>DSTStart && d<DSTFinish)
        d = d.AddHours(1);

    return (d);
}

答案 1 :(得分:1)

var israelTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "Israel Standard Time")

可以找到完整的时区 ID 列表 here