2

How to get current time of different countries in C# by using following any parameters, 1.country name 2.Timezone of country

This is for Windows / WPF Applications, not like Web Could anyone please answer this? Thanks in advance.

2 Answers2

6

You should look at TimeZoneInfo class, specifically at TimeZoneInfo.ConvertTime method.

For example, to get current time in Central Europe:

var remoteTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var remoteTime = TimeZoneInfo.ConvertTime(DateTime.Now, remoteTimeZone);
Console.WriteLine("Time in {0} is {1}", remoteTimeZone.Id, remoteTime.TimeOfDay.ToString());
Krzysztof Kozielczyk
  • 5,887
  • 37
  • 28
  • 1
    This does not provide the current timezone of a country, it just allows you to get information of a known timezone. – Frayt Sep 27 '21 at 10:29
  • The question is about getting time zone information, based on country name or similar identifier, not about getting the local time zone of the process or host. – Krzysztof Kozielczyk Sep 28 '21 at 04:32
-1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Example
{
    public static void Main()
    {

        DateTime thisTime = DateTime.Now;

        Console.WriteLine("Enter the Country");
        var Input= Console.ReadLine();
        string InputConcatenate = (Input + " Standard Time");

        TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById(InputConcatenate);
        DateTime tstTime = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst);
        Console.WriteLine("Time in {0} zone: {1}", tst.IsDaylightSavingTime(tstTime) ?
                          tst.DaylightName : tst.StandardName, tstTime);
        Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(tstTime, tst));

        Console.ReadLine();
    }
}
Mayur Narula
  • 150
  • 1
  • 4