0

I need to know Jerusalem Current time.

That code taking server time but I need Jerusalem time:

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

Edit:

with help of Vinoth answer(I took only the AddHours(2) part) it should be like that (not works):

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

Edit2:my improvement (ToUniversalTime())

      DateTime currentTime = DateTime.Now;
      currentTime=currentTime.ToUniversalTime().AddHours(2);//Jerusalem Time
      dayName = currentTime.DayOfWeek;
Ben
  • 25,389
  • 34
  • 109
  • 165
  • 1
    What have you [tried](http://stackoverflow.com/questions/246498/creating-a-datetime-in-a-specific-time-zone-in-c-sharp-fx-3-5)? – CodeCaster Oct 05 '12 at 13:09
  • All this code will do is get you the time two hours from now. You need an answer like @Vinoth's that uses the timezone. – neontapir Oct 05 '12 at 14:11

2 Answers2

4

This will help you. I have used this is one of my app. Jus pasting the code

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);
}
Vinoth
  • 2,419
  • 2
  • 19
  • 34
  • DateTime currentTime = DateTime.Now; currentTime=currentTime.AddHours(2);//Jerusalem Time dayName = currentTime.DayOfWeek do you meen for that? – Ben Oct 05 '12 at 13:54
  • That would get you the current day of the week according to Hebrew calender – Vinoth Oct 05 '12 at 13:59
  • my code handles when you are in the end of year. that extra handling made the long code.. – Vinoth Oct 05 '12 at 14:19
  • Why it should handle the end of the year? – Ben Oct 12 '12 at 10:09
2

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

Complete time zones ids list can be found here.

Nir
  • 1,836
  • 23
  • 26
  • I get "The time zone ID 'Israel Standard Time' was not found on the local computer.Could not find file '/usr/share/zoneinfo/Israel Standard Time'. From AWS – Rony Tesler Jun 26 '21 at 20:24