获取天时分之间的时间间隔,返回天时分格式的日期,利用一天1440分钟
写了老半天,还是电脑写的更简单,原来split还可以这么用(可以为数组),学到了。记录下
//我写的
public static string RetrieveSpanTimeByTime(string firstTime, string endTime)
{
int firstDayIndex = firstTime.IndexOf("天");
int endDayIndex = endTime.IndexOf("天");
int firstHourIndex = firstTime.IndexOf("时");
int endHourIndex = endTime.IndexOf("时");
int firstMinuteIndex = firstTime.IndexOf("分");
int endMinuteIndex = endTime.IndexOf("分");
int firstDay = firstDayIndex >= 0 ? firstTime.Substring(0, firstDayIndex).ToInt32() : 0;//获取天数
int endDay = endDayIndex >= 0 ? endTime.Substring(0, endDayIndex).ToInt32() : 0;
int firstHour = firstHourIndex >= 0&& firstDayIndex >= 0?firstTime.Substring(firstDayIndex + 1, firstHourIndex - firstDayIndex - 1).ToInt32():firstHourIndex >= 0 ? firstTime.Substring(0, firstHourIndex).ToInt32() : 0;//获取时前面的数字
int endHour = endHourIndex >= 0 && endDayIndex >= 0 ? endTime.Substring(endDayIndex + 1, endHourIndex - endDayIndex - 1).ToInt32() : endHourIndex >= 0 ? endTime.Substring(0, endHourIndex).ToInt32():0;
int firstMinute = firstHourIndex >= 0 ? firstTime.Substring(firstHourIndex + 1, firstMinuteIndex - firstHourIndex - 1).ToInt32() : firstDayIndex >= 0 ? firstTime.Substring(firstDayIndex + 1, firstMinuteIndex- firstDayIndex-1).ToInt32() : firstTime.Substring(0, firstMinuteIndex).ToInt32();//获取分前面的数字
int endMinute = endHourIndex >= 0 ? endTime.Substring(endHourIndex + 1, endMinuteIndex - endHourIndex - 1).ToInt32() : endDayIndex >= 0? endTime.Substring(endDayIndex + 1, endMinuteIndex- endDayIndex-1).ToInt32(): endTime.Substring(0, endMinuteIndex).ToInt32();
int firstTotleMinute = firstDay * 1440 + firstHour * 60 + firstMinute;//先转为分钟
int endTotleMinute = endDay * 1440 + endHour * 60 + endMinute;
int leftMinuteTime = firstTotleMinute - endTotleMinute;//总的时长-打回的时长
string returnDay = string.Empty;
string returnHour = string.Empty;
string returnMinute = string.Empty;
if (leftMinuteTime / 1440 != 0)//转为天,时,分的格式
{
returnDay = leftMinuteTime / 1440 + "天";
}
if (leftMinuteTime % 1440 / 60 != 0)
{
returnHour = leftMinuteTime % 1440 / 60 + "时";
}
returnMinute = leftMinuteTime % 1440 % 60 + "分";
return returnDay + returnHour + returnMinute;
}
//电脑写的
using System;
public class TimeInterval
{
public static string GetTimeInterval(string startTime, string endTime)
{
// 分解时间成分
int[] startComponents = ParseTime(startTime);
int[] endComponents = ParseTime(endTime);
// 将时间转换为分钟
int startMinutes = startComponents[0] * 1440 + startComponents[1] * 60 + startComponents[2];
int endMinutes = endComponents[0] * 1440 + endComponents[1] * 60 + endComponents[2];
// 计算时间间隔
int intervalMinutes = endMinutes - startMinutes;
// 处理负时间间隔
if (intervalMinutes < 0)
intervalMinutes += 1440 * 2; // 加两天的分钟数
// 将分钟转换回天时分
int days = intervalMinutes / 1440;
int hours = (intervalMinutes % 1440) / 60;
int minutes = intervalMinutes % 60;
// 返回结果
return $"{days}天{hours}时{minutes}分";
}
private static int[] ParseTime(string time)
{
string[] parts = time.Split('天', '时', '分');
return new int[]
{
int.Parse(parts[0]),
int.Parse(parts[1]),
int.Parse(parts[2])
};
}
}
// 使用示例
class Program
{
static void Main()
{
string startTime = "1天5时30分";
string endTime = "2天6时45分";
string interval = TimeInterval.GetTimeInterval(startTime, endTime);
Console.WriteLine(interval); // 输出: 1天1小时15分
}
}