1

I generate a datetime value from my javascript client to UTC format I need to be able to convert this datetime value into the date and time of particular culture.

I do this to get datetime

new Date().toUTCString(); // "Tue, 13 Jun 2017 07:44:58 GMT"

In my C# console application

Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
var dt = DateTime.Parse("Tue, 13 Jun 2017 07:44:58 GMT", Thread.CurrentThread.CurrentCulture);
Console.WriteLine(dt);

I always get the datetime displayed in the time of my zone rather then the cultureinfo value I pass to it.

What I need is when I parse a universal time with a particular culture is to show me the date and time of that particular cultureinfo (danish in the above code). How do I go about this?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
user581157
  • 1,327
  • 4
  • 26
  • 64
  • Hmmm, May be search bit more , this question is asked many times. – faheem khan Jun 13 '17 at 08:15
  • 3
    A `CultureInfo` isn't related to a time zone. Simple example: "en-US" - which of the many US time zones would you expect that to refer to? – Jon Skeet Jun 13 '17 at 08:17
  • 3
    I would steer clear of that text format, too - I'd strongly recommend formatting the value as an ISO-8601 string, or even just passing the milliseconds-since-the-unix-epoch value from Javascript. – Jon Skeet Jun 13 '17 at 08:18
  • DateTime is stored in computer as a number in UTF. The computer uses TimeZone setting to automatically convert any DateTime string to local time. So when you specify your input as GMT it is stored directly as GMT and bypasses the TimeZone setting when it is input. But when you output the same DateTime without a TimeZone value Net defaults to converting to local time. – jdweng Jun 13 '17 at 08:26

3 Answers3

2

Try

// See - https://stackoverflow.com/a/7908482/1603275 for a fuller list of options
TimeZoneInfo sourceTimeZone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");

// Not sure if DateTime.UtcNow will default to DateTimeKind.Utc
DateTime utcDate = DateTime.UtcNow;
utcDate = DateTime.SpecifyKind(utcDate, DateTimeKind.Utc);

DateTime localDate = TimeZoneInfo.ConvertTimeFromUtc(utcDate, sourceTimeZone);
Console.WriteLine(localDate);
Kami
  • 19,134
  • 4
  • 51
  • 63
-1

i suppose that you would this: https://msdn.microsoft.com/en-us/library/5hh873ya(v=vs.90).aspx

C.Fasolin
  • 313
  • 1
  • 4
  • 19
-1

It may help full

        System.Globalization.CultureInfo customCulture = new System.Globalization.CultureInfo("da-DK", true);
         //to change the date time patern
        //customCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd h:mm tt";

        System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
        System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture;

        DateTime newDate = System.Convert.ToDateTime("Tue, 13 Jun 2017 07:44:58 GMT");
Abinash
  • 471
  • 3
  • 13