0

How to display datetime format as per specific timezone.

24-02-2018 07:30:00

Means If one timezone date read as DD-MM-YY and in another It read as MM-DD-YY then how to show date as per timezone

I only have timezone name with me, I wanted to send email with few details. In that email one field is 'Last Updated time' So depending upon timezone I want to set that field in specific format, So In short I need way to get Culture code from TimeZone, Is it possible?

DevPerson
  • 399
  • 1
  • 6
  • 21
  • 1
    https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings – Svek May 03 '18 at 04:56
  • 1
    Try setting `Culture` in `global.asax`. You can search over internet for `Culture`. – DhavalR May 03 '18 at 04:57
  • @DhavalR I only have timezone name with me, I wanted to send email with dew details. In that email one field is 'Last Updated time' So depending upon timezone I want to set that field in specific format, So In short I need way to get Culture code from TineZone, Is it possible? – DevPerson May 03 '18 at 05:19
  • You need to save time zone names and related culture info somewhere, either in resource or web.config. In key-value manner. – DhavalR May 03 '18 at 05:27
  • @Kcsss are you following 24 hour format or 12 hour format? – Suvethan Nantha May 03 '18 at 05:27
  • I don't think you can: TimeZones and date formatting are two distinct concepts. Take as example [UTC-5](https://en.wikipedia.org/wiki/List_of_UTC_time_offsets#UTC%E2%88%9205:00,_R): it is used both in eastern US and in Brazil. in Brazil standard date format is DD-MM-YYYY, in US is MM-DD-YYYY. – Gian Paolo May 03 '18 at 05:35

3 Answers3

0

You need to set the culture in web.config to set the specific Date format.

<globalization uiCulture="es" culture="es-MX" />

You can find all cultureCodes here

And if you need that for specific conditions you may do like this,

var usCulture = "en-US";
var dateValue = DateTime.Parse(dateString, new CultureInfo(usCulture, false));
Smit Patel
  • 2,992
  • 1
  • 26
  • 44
  • I only have timezone name with me, I wanted to send email with dew details. In that email one field is 'Last Updated time' So depending upon timezone I want to set that field in specific format, So In short I need way to get Culture code from TineZone, Is it possible? – DevPerson May 03 '18 at 05:19
  • Explore this question, https://stackoverflow.com/questions/9869051/how-to-convert-datetime-in-specific-timezone?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Smit Patel May 03 '18 at 05:30
0

Always save the time in one time zone may be UTC in your database. Every time time is returned from your method, Convert to the client UI culture and show the same.

TimeZoneInfo targetTimeZone = TimeZoneInfo.FindSystemTimeZoneById("TimeZoneName");
DateTime utcTime = DateTime.Now();// your datetime object
TimeZoneInfo.ConvertTimeFromUtc(utc, targetTimeZone);
  • TimeZone and UTC offset is a thing, Culture and date formatting (e.g DD-MM-YYYY vs MM-DD-YYYY) is another. As far as I know, there is no build in connection with .Net framework, and I suspect it's not even possible to create one being always meaningful: in the same timezone there are people (and countries) using one Culture and other countries using different one. Here the problem is not converting an UTC date time to a different local date time, it is to properly format its string rapresentation according to what each user expect – Gian Paolo May 03 '18 at 17:50
  • Yes. If we can fetch the client culture info. We should be able to that. – raj barath May 04 '18 at 08:24
0

Try this

string datetime = "24-02-2018 07:30:00";
DateTime databaseUtcTime = DateTime.ParseExact(datetime,"dd-MM-yyyy HH:mm:ss",null);
Console.WriteLine("System Date : {0}", databaseUtcTime);
var japaneseTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
var japaneseTime = TimeZoneInfo.ConvertTimeFromUtc(databaseUtcTime, japaneseTimeZone);
Console.WriteLine("Japan Date : {0}", japaneseTime);

CSharp DateTime Formats

Since you have the timezone name, you can handle multiple timezones with the above solution. I hope this will help you. If you have any issues let me know.

Suvethan Nantha
  • 2,404
  • 16
  • 28