Reputation: 575
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
?
Upvotes: 0
Views: 2300
Reputation: 423
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.
Upvotes: 2
Reputation: 8592
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.
Upvotes: 0