Mackintoast
Mackintoast

Reputation: 1437

Get the current system datetime

I use my "epoch" as

Datetime myepoch=new DateTime(2005,11,11,7,0,0);

But I'd like it to be compatible with all the systems my exe intends to run on (machines in Asia and machines in Europe or America, for example, that is to change myepoche appropriately with the Datetime.Now). What culture class should I look into ?

Upvotes: 1

Views: 216

Answers (2)

Rich O'Kelly
Rich O'Kelly

Reputation: 41767

You'll be best served using an invariant culture as your epoch - eg UTC:

Datetime myEpoch = new DateTime(2005, 11, 11, 7, 0, 0, DateTimeKind.Utc);

Using an invariant culture makes reasoning about times and dates far simpler.

If you are going to be doing a lot of work around dates and times, I'd recommend having a look at the noda time blog for some of the lessons they've learned as well as possibly using some of the libraries that they've created.

Upvotes: 5

Andreas Rohde
Andreas Rohde

Reputation: 609

Work with a DateTime object, with that you can get both local and universal time

var tmpTime =  DateTime.Now;
var localTime = tmpTime.ToLocalTime();
var universalTime = tmpTime.ToUniversalTime();

Upvotes: 2

Related Questions