Calculate relative time in C#

后端 未结 30 2195
生来不讨喜
生来不讨喜 2020-11-21 05:59

Given a specific DateTime value, how do I display relative time, like:

  • 2 hours ago
  • 3 days ago
  • a month ago
30条回答
  •  既然无缘
    2020-11-21 06:17

    In PHP, I do it this way:

     604800) {
        $print = date("M jS", $original);
    
        if($since > 31536000) {
            $print .= ", " . date("Y", $original);
        }
    
        return $print;
    }
    
    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
    
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];
    
        // finding the biggest chunk (if the chunk fits, break)
        if (($count = floor($since / $seconds)) != 0) {
            break;
        }
    }
    
    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
    
    return $print . " ago";
    
    } ?>
    

提交回复
热议问题