我试图计算两个给定时间段的时差,但答案似乎不正确我做错了什么?
我的代码:

出于某种原因,给出的值amFinish从16:30改为16:18:00我不明白为什么!
如果我有一个文本框并且用户输入16.30怎么办这个值并将其计算为16小时30分钟怎么办?
答案应该是05.30,但我得到05.18.任何sugestions?
我有以下代码,当前给我一个时间跨度列表,每小时/分钟(24小时格式).我需要更改string.format以显示小时/分钟AM或PM(12小时格式).
var availableTimes =
_appointmentService.GetAvailableHours(date, appointmentId)
.Select(x => string.Format("{0:D2}:{1:D2}", x.Hours, x.Minutes));
Run Code Online (Sandbox Code Playgroud)
最好的方法是怎样做的?我没有看到任何时间跨度.
*这是它使用的GetAvailableHours方法.
public IEnumerable<TimeSpan> GetAvailableHours(DateTime? date, int? appointmentId)
{
if (date == null) return null;
var hours = new List<DateTime>();
for (var ts = new TimeSpan(); ts <= new TimeSpan(23, 30, 0); ts = ts.Add(new TimeSpan(0, 30, 0)))
{
hours.Add(date.Value + ts);
}
var booked = _appointmentRepository.Get
.Where(x =>
(!appointmentId.HasValue || x.Id != appointmentId))
.Select(x => x.ScheduledTime).ToList();
//return available hours from shifts
var workingHours = from h in hours …Run Code Online (Sandbox Code Playgroud) 嗨,我在 C# 中遇到了 TimeSpan 问题。
在我的应用程序中,我计算了两次时间的差异。这是怎么回事->
12:00:30 - 12:00:00 = 00:00:30
Run Code Online (Sandbox Code Playgroud)
但我的问题是如果第二次比第一次大...我得到一个负数:(
1:00:00 - 23:00:00 = -22:00:00
Run Code Online (Sandbox Code Playgroud)
但我想要一个正数,这是怎么回事 -->
1:00:00 - 23:00:00 -> 2:00:00
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
private static int GetTimeSpan(string Out, string In) {
try
{
TimeSpan diff = DateTime.Parse(In) - DateTime.Parse(Out);
double TotalSec = diff.TotalSeconds;
return (int)TotalSec;
}
catch (Exception)
{
return 0;
}
}
Run Code Online (Sandbox Code Playgroud) 继续我已经安排的代码,但仍然有相同的问题,无法在最后一页找到我的开始和结束时间?我该怎么做呢?
public void start()
{
DateTime startTime = DateTime.Now;
}
protected void btnStart_Click(object sender, EventArgs e)
{
start();
Response.Redirect("~/end.aspx");
}
Run Code Online (Sandbox Code Playgroud)
public void end()
{
DateTime endTime = DateTime.Now;
}
protected void btnEnd_Click(object sender, EventArgs e)
{
end();
Response.Redirect("~/display.aspx");
}
Run Code Online (Sandbox Code Playgroud)
public partial class display : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TimeSpan timeSpent = endTime - startTime;
lblDisplay.Text = string.Format("Time: {0}", timeSpent);
}
}
Run Code Online (Sandbox Code Playgroud)
现在有人可以帮我吗?
我有两个时间跨度,我想在第一个时间点添加第二个时间跨度:
TimeSpan weeklyWorkTimeHours = new TimeSpan(0,0,0);
TimeSpan? completeWorkTimeForCurrentDay =
CalculateCompleteWorktime(currentWorkTimeItem).Value; /* I debugged through
the code. This method returns a correct timespan with a correct value */
weeklyWorkTimeHours.Add(completeWorkTimeForCurrentDay.Value);
Run Code Online (Sandbox Code Playgroud)
但即使在最后一行代码之后,weeklyWorkTimeHours也包含0,0,0.为什么不在此上下文中添加工作?
我需要在 C# 中计算一个操作的通过时间和剩余时间。我以 HH:MM:SS 的字符串格式保存了操作的开始
我有一个 HH:MM:SS 字符串格式的默认操作时间长度
现在我想计算:
两个结果将彼此相邻显示。我知道我必须将字符串值转换为 DateTime 和/或 TimeSpan 值才能进行计算,但目前我不知道如何计算,因为例如第一个操作不会给我一个负值,但只是及时返回 [昨天 22:30:00]。
我想将Ticks转换为TimeSpan.
我需要一个ConvertToTimeSpan像下面这样的功能.
var ticks = 10000;
TimeSpan ts = ConvertToTimeSpan(ticks); // not working
Console.WriteLine(ts); // expected output --> {00:00:00.0010000}
Run Code Online (Sandbox Code Playgroud) 我已经看到了如何将a转换string为a的TimeSpan示例,这是一个例子:
如何将字符串"07:35"(HH:MM)转换为TimeSpan
但是,什么是最有效的转换方式List<string>来List<TimeSpan>?
我已经尝试过这些方法,但是没有用:
var result = new TimeSpan;
var appointmentStartTimesConverted = appointmentStartTimes
.Select(i => result = TimeSpan.TryParse(i, out result))
.ToList();
Run Code Online (Sandbox Code Playgroud) 嗨,我必须执行总小时数,为此我使用下面的代码 c#:首先将 hourtot 转换为时间跨度然后我执行总和,最后总和我得到 08:30,这是不可能的,我怎么能解决这个?这是因为什么?
C# 代码:
/*
hourtot values:
08:30
10:00
09:00
08:30
10:00
10:30*/
TimeSpan tot= TimeSpan.Zero;
foreach (DataRow dr in dt.Rows)
{
String hourtot= r.CaricaOreGiornaliere(dr["date"].ToString());
if(hourtot.Equals("00:00") == false)
{
TimeSpan hourcant= TimeSpan.Parse(hourtot.ToString());
tot= tot+ hourcant;
}
}
labelris.text = "" + tot.ToString(@"hh\:mm"); //this print 08:30
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 TimeSpan 乘以一个系数,但我不知道该怎么做。
我试过这个:
long ErrorCoef = 25;
TimeSpan TotalTimer = new TimeSpan(10,1,2,0);
TimeSpan TotalTimer2 = TimeSpan.FromTicks(TotalTimer.Ticks + TotalTimer.Ticks * (ErrorCoef / 100));
Run Code Online (Sandbox Code Playgroud)
但(ErrorCoef/100)自动转换为 Long,所以它返回 0。
(TimeSpan.FromTicks() 方法只接受 Long 而不是 Double)