0

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

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

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;
4

2 回答 2

4

这将对您有所帮助。我用过这是我的应用程序之一。只需粘贴代码

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);
}
于 2012-10-05T13:09:38.793 回答
1

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

可以在此处找到完整的时区 ID 列表。

于 2021-02-02T13:59:26.620 回答