-1

I'm looking to find the days between two datetime values.

On my Linux home machine, it works fine, and returns 13 (rounding up). On the Linux VM (GCP) it's returning -46?

Why is this?

Math.Round((DateTime.Now - DateTime.Parse("10/08/2023 06:00:00")).TotalDays)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ALS
  • 25
  • 5

3 Answers3

0

I'm not sure why you are saying it returning 13 is correct for your linux machine? Is the DateTime on that machine set correctly? It should be -46. It should be 13 after reading the comment, I read it in the wrong date format culture.

Lets define your two dates (using the datetime from my response) as

  • Start = 08/22/2023 11:00:00
  • End = 10/08/2023 06:00:00

You are subtracting a previous date from a future date so you will end up with a negative value. You can run this code or do a fiddle to see the output.

    var now = DateTime.Parse("08/22/2023 11:00:00");
    var future = DateTime.Parse("10/08/2023 06:00:00");
    var difference = now - future;
    Console.WriteLine(difference.TotalDays);

EDIT: I read the date in US format, I didn't see the comment specifying he is targeting August 10th, not October 8th. In this case you will need to specify the format that you are providing to DateTime, I'm guessing that on your local machine (Linux Home Machine) it handles the date time parsing in that specific format you specified but when you put it on the cloud (Linux VM on GCP) its in another format.

You can use DateTime.ParseExact() to specify the format, this below code generates the correct difference.

    var now = DateTime.ParseExact("22/08/2023 11:00:00", "dd/MM/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
    var futureExact = DateTime.ParseExact("10/08/2023 06:00:00", "dd/MM/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
    var differenceExact = now - futureExact;
    Console.WriteLine(differenceExact.TotalDays);
    Console.WriteLine("Rounded: " + Math.Round(differenceExact.TotalDays));

This will return 12.208 which you can round however you like

97WaterPolo
  • 375
  • 1
  • 3
  • 24
  • 1
    You are assuming the funny american date order MM/dd/yyyy, right? The OP seems to have a different culture. – Klaus Gütter Aug 22 '23 at 19:58
  • If that's the case you will need to update the format so you can parse it in a specific way (given the month and day are swapped). I'll update my answer – 97WaterPolo Aug 22 '23 at 20:03
  • @KlausGütter Yeah pretty funny https://www.reddit.com/r/ShitAmericansSay/comments/xyif31/july_4th_which_is_how_i_hear_the_majority_of/ – Charlieface Aug 23 '23 at 11:16
0

There is a function called Subtract built into the DateTime class that let's you compute the accurate interval between any two dates without the need to round off.

var date1 = DateTime.Parse("10/08/2023 06:00:00");
var days = DateTime.Now.Subtract(date1).Days;
Son of Man
  • 1,213
  • 2
  • 7
  • 27
-1

Three actions you can try to find a solution:

  • The problem might be in timezones. There is a big chance that your local machines timezone and vms is different and when you use DateTime.Now you get that error.
  • You can try using UTC. Instead of DateTime.Now use DateTime.UtcNow.
  • If these above suggestions don't work, I advise you to debug and log every single detail you have.
mxmissile
  • 11,464
  • 3
  • 53
  • 79