简体   繁体   中英

How to display datetime format as per specific timezone (DD-MM-YY) / (MM-DD-YY) in C#

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?

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));

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM