Boris
Boris

Reputation: 8941

How to set "local" for DateTime.ToLocalTime?

How does .NET determine where I am? How can I manipulate my current location and thus the behavior of this method?

The reason is I'm using .NET MF on a microcontroller board. Here I don't have any operating system. Still I want to convert times to my local time.

Upvotes: 1

Views: 413

Answers (2)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Try This:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");//culture name

Upvotes: 1

Saverio Terracciano
Saverio Terracciano

Reputation: 3915

There are basically two ways, you either set the CurrentCulture for your whole instance

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE"); //For German

or you can just use it to format your outputs like:

DateTime dt = DateTime.Now; 
Console.WriteLine(dt.ToString("d",new CultureInfo("de-DE"))); //For German

Upvotes: 1

Related Questions