Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to convert milliseconds to a human readable date

Status
Not open for further replies.

kognitio

Programmer
Apr 3, 2004
11
DE
Hello together,
I am getting a time-value from a machine in milliseconds from 1970 on. How can I convert this in a humanreadable date like;
Code:
DateTime.Now
Maybe somone can help me directly:
Code:
sbyte []state = new sbyte[100];
ds1994.getClock(state);
state = ds1994.readDevice();
label10.Text = ds1994.getClock(state).ToString();
I can convert a normal time to milliseconds from 1970:
Code:
DateTime neu = DateTime.Now;
TimeSpan zeit = (neu - new DateTime(1970, 1, 1));
label14.Text = zeit.TotalMilliseconds.ToString();
Is the no function back, like "fromMillisecondsToDate"?
Thanks alot for helping.
Best,
Stephan
 
You mean you need something like the following?

DateTime m_dtDateTime = new DateTime( 1970, 1, 1, 0, 0, 0, 0); // In order to start from 1/1/1970 00:00.00
m_dtDateTime.AddMilliseconds( zeit.TotalMilliseconds);
label14.Text = m_dtDateTime.ToString()
 
well ... thanks. that is working.
but not at all. I am getting the wrong years, means I am getting "0036", which is logical, because it is counting the years from 1970 to today. But ho can I get the correct years???
this is the code:
Code:
double we = 0;
double rt = 0;
we = Convert.ToDouble(ds1994.getClock(state));
rt = Convert.ToDouble(ds1994.getClockAlarm(stateAlarm));
long millis = (long) rt;
long mill = (long) we;
DateTime alarm = new DateTime(millis * 10);
DateTime clock= new DateTime(mill * 10);
				
label10.Text = clock.AddMilliseconds(mill).ToString();
				

label11.Text = alarm.AddMilliseconds(millis).ToString();
thanks alot for helping.
Best,
Stephan
 
You can call the AddYears() method to add 1970 back in.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
If you have the time in milliseconds counted from 1970,01,01 then the following line will give you the date and time:
Code:
DateTime dt = new DateTime( new DateTime(1970,01,01).Ticks + mssince1970*TimeSpan.TicksPerMillisecond);

if mssince1970 = 946684800000 then you should get 01/01/2000.
obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top