-2

My problem is the following, I pull a string of raw data from a website and it contains a value in milliseconds of a time in the future. Now I want to calculate how much time is remaining until that time in the future. First I tried converting the current time to milliseconds and I also tried the opposite of trying to convert the string of milliseconds to a time value I can use but none of these worked.. This is probably a very basic question but I'm new and trying to learn.

Following snip of code is what i was working with.

DateTime baseDate = new DateTime(1970, 1, 1);   
long dissapearTime = long.Parse(test.disappear_time.ToString()); 
TimeSpan diff = DateTime.Now - baseDate;
Console.WriteLine(diff.TotalMilliseconds-dissapearTime);
string oeps = diff.TotalMilliseconds.ToString();

my string remains empty though..

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • What is `dissapearTime`? Where is it defined? – ProgrammingLlama Jan 31 '18 at 07:55
  • `long dissapearTime = long.Parse(test.disappear_time.ToString());` its a string of milliseconds pulled from the raw data of a website. – Duofilm Jan 31 '18 at 07:58
  • _"my string remains empty though"_ -- which string? `oeps`? No, the code above cannot possibly do that. The `TotalMilliseconds` property has type `double`. Converting that `double` value to a `string` with `ToString()` will _always_, without fail, return a `string` of non-zero length. You need to fix your question so it includes a good [mcve] that reliably reproduces your problem. – Peter Duniho Jan 31 '18 at 08:02
  • Why are you converting `disappear_time` to a string and then parsing it? It would really help if you'd provide a [mcve] with expected input and output rather than just snippets. – Jon Skeet Jan 31 '18 at 08:02
  • 2
    Additionally, I expect you'll want to use UTC everywhere - both for the Unix epoch and for the current instant in time. – Jon Skeet Jan 31 '18 at 08:03
  • I'm very new to all this, i have also never posted a question here before my apologies. – Duofilm Jan 31 '18 at 08:41

1 Answers1

2

The easiest solution would be to convert the time to a proper DateTime obejct and use the standard DateTime calculation.

// 1ms = 10000 ticks, there may be a constant for that in DateTime, but I am not 100% sure
DateTime disappearTime = new DateTime(long.Parse(test.disappear_time.ToString()) * 10000);
Console.WriteLine(disappearTime - DateTime.Now);

Be aware that this conversion from milliseconds starts counting at 1st January of the year 1. So in case these milliseconds start from another date (you were mentioning the 1st of January 1970), you would have to subtract that date as well.

Georg
  • 5,626
  • 1
  • 23
  • 44
  • Side note: 1st January of the year **1**; We did't have year `0`: it was `1 AD` after `1 BC` – Dmitry Bychenko Jan 31 '18 at 08:45
  • I'm sorry for what possibly is yet another simple question but the result of the deduction you do is a timespan. I learned as much that I can't deduct another datetime from that. Where should I deduct the other date being 1 jan 1970 ? – Duofilm Jan 31 '18 at 08:49
  • @Duofilm You would have to convert the date to a timespan, but actually you can as well create the disappearTime as a `TimeSpan` and then add this time span to the reference date in order to subtract `DateTime.Now` and then the result still is a `TimeSpan` – Georg Jan 31 '18 at 14:04