-4

有大问题我不知道如何像 PHP 一样在 C# 中比较日期。我想将此代码转换为 C#:

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

我应该使用哪个功能? 此链接找不到类似的匹配函数strtotime

请帮我将代码转换为 C#。提前致谢。

4

2 回答 2

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 

基本上

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

希望能给你更好的理解

于 2013-07-16T07:51:39.833 回答
0

我想你可能正在寻找类似下面的东西:

        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;
于 2013-07-16T07:58:30.333 回答