Setting UTC time to 1970 (Unix Epoch)

  • Thread starter Thread starter Maziar Aflatoun
  • Start date Start date
M

Maziar Aflatoun

Hi,

Can someone please tell me how I can set DateTime to 01/01/1970 (UTC).
Doing the following

DateTime dt1 = new DateTime(1970, 1,1);
Debug.WriteLine("dt1(utc):"+ dt1.ToUniversalTime().ToString());

Doing the following returns 1/1/1970 5:00:00 AM

However, I want to set the DateTime so that it would return 1/1/1970
12:00:00 AM instead.

Basically force DateTime(1970,1,1) to be UTC.

Thanks
Maz.
 
Maziar,

Common mistake is that programmers assume DataTime objects has some relation
to some time zone. There is no such thing. DateTime wraps one double number
and that is what it knows all about. Thre are couple of methods and
properties that take into account the time zone, but information inside has
nothig to do with any time zones.

So the answer of your question is pretty simple:
DateTime dt1 = new DateTime(1970, 1,1);
Debug.WriteLine("dt1(utc):"+ dt1.ToString());

Jan 1st, 1970 00:00 is the same in any time zone.

If you want to het the current time in unix like format as milliseconds
since Jan 1st, 1970 you can do:

(DateTime.UtcNow - new DateTime(1970, 1,1)).TotalMilliseconds.ToString();

UtcNow property is one of few that take into accound the current time zone.
 
Stoitcho & Maziar,
| There is no such thing. DateTime wraps one double number
| and that is what it knows all about.
Actually DateTime wraps one Int64 number.
http://msdn2.microsoft.com/03ybds8y(en-US,VS.80).aspx

I suspect you are thinking of COM DateTime values.

| but information inside has
| nothig to do with any time zones.
Yes & No, a DateTime can represent local time, UTC time or unspecified,
Starting with .NET 2.0 there the DateTime.Kind property tells you which the
DateTime represents. Although .NET 1.0 & 1.1 could be either, it was not
obvious which a specific DateTime was (effectively
DateTimeKind.Unspecified).

http://msdn2.microsoft.com/e3t6hfy5(en-US,VS.80).aspx

With .NET 2.0 you can indicate that you want a UTC DateTime (or Local
DateTime, or Unspecified), something like:

DateTime dt1 = new DateTime(1970, 1,1, 0, 0, 0,
DateTimeKind.Utc);
Debug.WriteLine(dt1.ToUniversalTime(), "dt1(utc)");


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Maziar,
|
| Common mistake is that programmers assume DataTime objects has some
relation
| to some time zone. There is no such thing. DateTime wraps one double
number
| and that is what it knows all about. Thre are couple of methods and
| properties that take into account the time zone, but information inside
has
| nothig to do with any time zones.
|
| So the answer of your question is pretty simple:
| DateTime dt1 = new DateTime(1970, 1,1);
| Debug.WriteLine("dt1(utc):"+ dt1.ToString());
|
| Jan 1st, 1970 00:00 is the same in any time zone.
|
| If you want to het the current time in unix like format as milliseconds
| since Jan 1st, 1970 you can do:
|
| (DateTime.UtcNow - new DateTime(1970, 1,1)).TotalMilliseconds.ToString();
|
| UtcNow property is one of few that take into accound the current time
zone.
|
|
| --
| HTH
| Stoitcho Goutsev (100) [C# MVP]
|
| | > Hi,
| >
| > Can someone please tell me how I can set DateTime to 01/01/1970 (UTC).
| > Doing the following
| >
| > DateTime dt1 = new DateTime(1970, 1,1);
| > Debug.WriteLine("dt1(utc):"+ dt1.ToUniversalTime().ToString());
| >
| > Doing the following returns 1/1/1970 5:00:00 AM
| >
| > However, I want to set the DateTime so that it would return 1/1/1970
| > 12:00:00 AM instead.
| >
| > Basically force DateTime(1970,1,1) to be UTC.
| >
| > Thanks
| > Maz.
| >
|
|
 
Jay,

Thanks for the correction.
Actually DateTime wraps one Int64 number.
http://msdn2.microsoft.com/03ybds8y(en-US,VS.80).aspx

I suspect you are thinking of COM DateTime values.

I should've check first. I was talking .NET 1.x. I don't know where I got
that probably some memories from some other frameworks I've used. It could
be COM or VCL that I used many years ago.

It looks you have .NET 2.0 in mind. If this is the case I'd correct you in
my turn. It looks like one of the changes that Microsoft's guys did, in .NET
2.0, is to change the value that DateTime wraps to UInt64 :).

Any ways whether is double, Int64 or UInt64 my point was that prior .NET 2.0
DateTime didn't actually care for the time zone.
| but information inside has
| nothig to do with any time zones.
Yes & No, a DateTime can represent local time, UTC time or unspecified,
Starting with .NET 2.0 there the DateTime.Kind property tells you which
the
DateTime represents. Although .NET 1.0 & 1.1 could be either, it was not
obvious which a specific DateTime was (effectively
DateTimeKind.Unspecified).

Yes, I wasn't aware of these new additions of the DateTime probably because
I've never have any problems with it, but giving the fact how many
probgrammers got confused of all this time-zone stuff I believe it is a good
thing to do.

So thanks again for the correction.


--

Stoitcho Goutsev (100) [C# MVP]

http://msdn2.microsoft.com/e3t6hfy5(en-US,VS.80).aspx

With .NET 2.0 you can indicate that you want a UTC DateTime (or Local
DateTime, or Unspecified), something like:

DateTime dt1 = new DateTime(1970, 1,1, 0, 0, 0,
DateTimeKind.Utc);
Debug.WriteLine(dt1.ToUniversalTime(), "dt1(utc)");


--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Maziar,
|
| Common mistake is that programmers assume DataTime objects has some
relation
| to some time zone. There is no such thing. DateTime wraps one double
number
| and that is what it knows all about. Thre are couple of methods and
| properties that take into account the time zone, but information inside
has
| nothig to do with any time zones.
|
| So the answer of your question is pretty simple:
| DateTime dt1 = new DateTime(1970, 1,1);
| Debug.WriteLine("dt1(utc):"+ dt1.ToString());
|
| Jan 1st, 1970 00:00 is the same in any time zone.
|
| If you want to het the current time in unix like format as milliseconds
| since Jan 1st, 1970 you can do:
|
| (DateTime.UtcNow - new DateTime(1970,
1,1)).TotalMilliseconds.ToString();
|
| UtcNow property is one of few that take into accound the current time
zone.
|
|
| --
| HTH
| Stoitcho Goutsev (100) [C# MVP]
|
| | > Hi,
| >
| > Can someone please tell me how I can set DateTime to 01/01/1970 (UTC).
| > Doing the following
| >
| > DateTime dt1 = new DateTime(1970, 1,1);
| > Debug.WriteLine("dt1(utc):"+ dt1.ToUniversalTime().ToString());
| >
| > Doing the following returns 1/1/1970 5:00:00 AM
| >
| > However, I want to set the DateTime so that it would return 1/1/1970
| > 12:00:00 AM instead.
| >
| > Basically force DateTime(1970,1,1) to be UTC.
| >
| > Thanks
| > Maz.
| >
|
|
 
Stoitcho,
| It looks you have .NET 2.0 in mind. If this is the case I'd correct you in
| my turn.
..NET 2.0 *is* the current version. ;-)


| It looks like one of the changes that Microsoft's guys did, in .NET
| 2.0, is to change the value that DateTime wraps to UInt64 :).
Interesting using ILDASM.EXE I see internally the DateTime now wraps an
UInt64 instead of an Int64, which if you think about it actually makes
sense.

However! That is an implementation detail. I consider both UInt64 & Int64 to
be 64-bit "integers", as oppose to a 64-bit floating point.

My initial statement was based on the Remarks - Version Considerations under
the link I gave initially, which tells you how the value is stored under 1.0
& 1.1 verses 2.0. Which is also why I gave the link, it tells you how its
stored in both versions.

http://msdn2.microsoft.com/03ybds8y(en-US,VS.80).aspx


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Jay,
|
| Thanks for the correction.
|
| > Actually DateTime wraps one Int64 number.
| > http://msdn2.microsoft.com/03ybds8y(en-US,VS.80).aspx
| >
| > I suspect you are thinking of COM DateTime values.
|
| I should've check first. I was talking .NET 1.x. I don't know where I got
| that probably some memories from some other frameworks I've used. It could
| be COM or VCL that I used many years ago.
|
| It looks you have .NET 2.0 in mind. If this is the case I'd correct you in
| my turn. It looks like one of the changes that Microsoft's guys did, in
..NET
| 2.0, is to change the value that DateTime wraps to UInt64 :).
|
| Any ways whether is double, Int64 or UInt64 my point was that prior .NET
2.0
| DateTime didn't actually care for the time zone.
| >
| > | but information inside has
| > | nothig to do with any time zones.
| > Yes & No, a DateTime can represent local time, UTC time or unspecified,
| > Starting with .NET 2.0 there the DateTime.Kind property tells you which
| > the
| > DateTime represents. Although .NET 1.0 & 1.1 could be either, it was not
| > obvious which a specific DateTime was (effectively
| > DateTimeKind.Unspecified).
|
| Yes, I wasn't aware of these new additions of the DateTime probably
because
| I've never have any problems with it, but giving the fact how many
| probgrammers got confused of all this time-zone stuff I believe it is a
good
| thing to do.
|
| So thanks again for the correction.
|
|
| --
|
| Stoitcho Goutsev (100) [C# MVP]
|
|
| >
| > http://msdn2.microsoft.com/e3t6hfy5(en-US,VS.80).aspx
| >
| > With .NET 2.0 you can indicate that you want a UTC DateTime (or Local
| > DateTime, or Unspecified), something like:
| >
| > DateTime dt1 = new DateTime(1970, 1,1, 0, 0, 0,
| > DateTimeKind.Utc);
| > Debug.WriteLine(dt1.ToUniversalTime(), "dt1(utc)");
| >
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > | > | Maziar,
| > |
| > | Common mistake is that programmers assume DataTime objects has some
| > relation
| > | to some time zone. There is no such thing. DateTime wraps one double
| > number
| > | and that is what it knows all about. Thre are couple of methods and
| > | properties that take into account the time zone, but information
inside
| > has
| > | nothig to do with any time zones.
| > |
| > | So the answer of your question is pretty simple:
| > | DateTime dt1 = new DateTime(1970, 1,1);
| > | Debug.WriteLine("dt1(utc):"+ dt1.ToString());
| > |
| > | Jan 1st, 1970 00:00 is the same in any time zone.
| > |
| > | If you want to het the current time in unix like format as
milliseconds
| > | since Jan 1st, 1970 you can do:
| > |
| > | (DateTime.UtcNow - new DateTime(1970,
| > 1,1)).TotalMilliseconds.ToString();
| > |
| > | UtcNow property is one of few that take into accound the current time
| > zone.
| > |
| > |
| > | --
| > | HTH
| > | Stoitcho Goutsev (100) [C# MVP]
| > |
| > | | > | > Hi,
| > | >
| > | > Can someone please tell me how I can set DateTime to 01/01/1970
(UTC).
| > | > Doing the following
| > | >
| > | > DateTime dt1 = new DateTime(1970, 1,1);
| > | > Debug.WriteLine("dt1(utc):"+ dt1.ToUniversalTime().ToString());
| > | >
| > | > Doing the following returns 1/1/1970 5:00:00 AM
| > | >
| > | > However, I want to set the DateTime so that it would return 1/1/1970
| > | > 12:00:00 AM instead.
| > | >
| > | > Basically force DateTime(1970,1,1) to be UTC.
| > | >
| > | > Thanks
| > | > Maz.
| > | >
| > |
| > |
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top