Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3172
  • Last Modified:

How convert UTC/GMT Time to Date Time in C#

I have a numeric field in sql 2000 server, numeric (10,0) not null.It represents the seconds from 1/1/1970.I want to convert this numeric to C# Date Time.
0
nikavak
Asked:
nikavak
  • 2
1 Solution
 
strickddCommented:
This will convert from DateTime to UCTTimestamp so you just need to inverse it.
DateTime temp = ((DateTime)reader["UTCTime"]).ToUniversalTime();
transactionInfo.UtcTimeStamp = (long)temp.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;

Open in new window

0
 
nikavakAuthor Commented:
I dont understand this solution.I remark that i have a numeric (10,0) sql server field and I want to convert it to local time.This Field means the seconds since 1/1/1970
0
 
nikavakAuthor Commented:
I found the following solution

long ccc = 1228893007; //FROM DATABASE
            DateTime temp = new DateTime(ccc *10000000);//for each tick=100 nanoseconds
            DateTime temp1 = new DateTime(1970, 1, 1,00,00,00);
           

           
            DateTime transactionInfo;
            transactionInfo = new DateTime(temp1.Ticks + (ccc*10000000));
            transactionInfo = transactionInfo.ToLocalTime();
           

Best regards,
nikavak
0
 
RSBuTCHeRCommented:
The solution is very simple. You know the ammount of seconds from 1-1-1970 till the date you want to show.
So you can just add the number of seconds to the date of 1-1-1970.

Best regards,

Falcko
            //Timestamp from database
            long timeStamp = 1000000000;
 
            //Start date (1-1-1970)
            DateTime startDate = new DateTime(1970, 1, 1);
 
            //Calculate the date
            DateTime endDate = startDate.AddSeconds(timeStamp);

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now