-4

Have big problem I don´t know how to compare date in C# like PHP. I want to convert this code in to C#:

$now = strtotime ("now");
$then = strtotime ("$date");
$difference = $now - $then ;
$num = ($difference/86400)/7;
$weeks = intval($num);

Which function should I use? This link can´t find a matching function like strtotime.

Please help me to convert the code to C#. Thanks in advance.

Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78

2 Answers2

0
var now = DateTime.Now.TimeOfDay;
DateTime dt = DateTime.Parse(yourDateTime);

var then = dt.ToString("HH:mm"); 

// here you can do rest of the calculations 

basically

dt.ToString("HH:mm");  // 24 hour clock  
dt.ToString("hh:mm tt"); // 12 hour clock 

Hope it will give you better understanding

Rajeev Bera
  • 2,021
  • 1
  • 16
  • 30
0

I think you might be looking for something like below:

        DateTime sDateTimeNow = DateTime.Now;//Gets the date time from the server now
        DateTime sIn30Seconds = DateTime.Now.AddSeconds(30);//Gets date time and adds 30 Seconds
        DateTime sIn30Hours = DateTime.Now.AddHours(30);//Gets date time and adds 30 hours
        DateTime sIn30Days = DateTime.Now.AddDays(30);//Gets date time and adds 30 days

        double dTotalDays = (sIn30Days - sDateTimeNow).TotalDays;
        double dTotalHours = (sIn30Hours - sDateTimeNow).TotalHours;
        double dTotalSeconds = (sIn30Seconds - sDateTimeNow).TotalSeconds;
BossRoss
  • 869
  • 2
  • 11
  • 31