Reputation: 151
i need to get Turkey's (Istanbul) date and time from a server in another country. So i can use it in my asp.net project and add it to my sql server database. Can you help me please ?
Upvotes: 8
Views: 16049
Reputation: 2435
Try this:
var info = TimeZoneInfo.FindSystemTimeZoneById("Turkey Standard Time");
DateTimeOffset localServerTime = DateTimeOffset.Now;
DateTimeOffset localTime= TimeZoneInfo.ConvertTime(localServerTime, info);
Upvotes: 15
Reputation: 8591
You can get Turkey time this way:
// Get time in local time zone
DateTime thisTime = DateTime.Now;
Console.WriteLine("Time in {0} zone: {1}",
TimeZoneInfo.Local.IsDaylightSavingTime(thisTime) ?
TimeZoneInfo.Local.DaylightName : TimeZoneInfo.Local.StandardName, thisTime);
Console.WriteLine(" UTC Time: {0}",
TimeZoneInfo.ConvertTimeToUtc(thisTime, TimeZoneInfo.Local));
// Get GTB Standard Time zone - (GMT+02:00) Athens, Istanbul, Minsk
TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("GTB Standard Time");
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));
For further information, please see:
TimeZoneInfo.FindSystemTimeZoneById Method
Microsoft Time Zone Index Values
Upvotes: 2