0

How can I retrieve the current date with time in

UTC format based on country code ?

For example, Afghanistan has ISO (country code) AF.

How to I pass this parameter AF in DateTime.UtcNow ?

crazydev
  • 575
  • 2
  • 9
  • 30

2 Answers2

2

As the answer above states, UTC is the timezone, it is not specific to any country. Indeed even in the UK where the line intersects, only half the year is the same value as UTC, the rest is British Summer Time.

But if your question is how to turn Afghanistan time into UTC but you are not currently set to that timezone then

// you can get all the timezones your system supports using TimeZoneInfo.GetSystemTimeZones()
var afghanTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Afghanistan Standard Time");
var utcTime = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1), afghanTimeZone);

Time Zones do not match one for one with countries, and even inside a country there can be many more than one, so country code alone will not cut it.

cineam mispelt
  • 393
  • 1
  • 8
  • Try not to use "positional" language to refer to other answers on SO. Due to voting, acceptance, randomization, and the ability for people to manually apply sorting, there's no guarantee that people in the future will be able to determine which answer is "the answer above" - instead I'd recommend *either* using the other answerer's username, *linking* to the other answer or both - e.g. [Adam's answer](http://stackoverflow.com/a/43974262/15498) – Damien_The_Unbeliever May 15 '17 at 09:16
0

UTC stands for Universal Time Co-ordinated. The whole point of it is that it is always the same, regardless of time zone or country. At the moment, in Britain the local time is 9:01 am as I type this, but the time in UTC is 8:01.

See https://www.timeanddate.com/time/aboututc.html

You are better off storing all timestamps in UTC and then converting them to local time for display.

So, to answer your question, you can't do it. Country and timezone are meaningless to UTC.

Hope this helps,

Adam.

Adam Benson
  • 7,480
  • 4
  • 22
  • 45
  • To give you sense of scale, when this was written, Local time in India was 13:31 while the time in UTC was still 8:01. (That's because India is 5.30 hrs ahead of UTC, while ATM Britain is 1 hr ahead of UTC) – Nikhil Agrawal May 15 '17 at 08:19