有没有工具类,计算2个日期相差分钟数
时间: 2024-03-09 08:46:33 浏览: 187
是的,有很多编程语言都提供了计算2个日期相差分钟数的工具类或函数。以下是几种语言的示例:
Java:
```java
import java.util.concurrent.TimeUnit;
import java.time.Duration;
import java.time.LocalDateTime;
public class DateUtils {
public static long getMinutesBetween(LocalDateTime start, LocalDateTime end) {
Duration duration = Duration.between(start, end);
return duration.toMinutes();
}
}
```
Python:
```python
from datetime import datetime
def get_minutes_between(start, end):
duration = end - start
minutes = int(duration.total_seconds() / 60)
return minutes
```
JavaScript:
```javascript
function getMinutesBetween(start, end) {
const duration = end - start;
const minutes = Math.floor(duration / (1000 * 60));
return minutes;
}
```
C#:
```csharp
using System;
public class DateUtils {
public static int GetMinutesBetween(DateTime start, DateTime end) {
TimeSpan duration = end - start;
return (int)duration.TotalMinutes;
}
}
```
注意,这些示例中的日期格式可能会因语言和使用情况而异。
阅读全文