Html 如何在blazor服务器应用程序的组件中使用timespan并将其转换为12小时格式
我的要求是添加时隙设置,编辑并显示该时隙 数据库中的字段包括: 1.身份证(简称) 2.FromTime(时间跨度) 3.ToTime(时间跨度) 4.显示时间(字符串) 模型类: 数据库读取: 剃刀页面Html 如何在blazor服务器应用程序的组件中使用timespan并将其转换为12小时格式,html,blazor,datetime-format,blazor-server-side,timespan,Html,Blazor,Datetime Format,Blazor Server Side,Timespan,我的要求是添加时隙设置,编辑并显示该时隙 数据库中的字段包括: 1.身份证(简称) 2.FromTime(时间跨度) 3.ToTime(时间跨度) 4.显示时间(字符串) 模型类: 数据库读取: 剃刀页面 时间洗液 不时 随时 显示文本 @item.DisplayText 我发现很难使用任何组件来读取、添加或编辑它的时间跨度。 我使用了datetimepicker,但我不知道如何在将其保存到数据库时将其转换为timespan。有人能帮我解决这个问题吗。 谢谢大家! 此解决方案绑定到设置模型时
时间洗液
不时
随时
显示文本
@item.DisplayText
我发现很难使用任何组件来读取、添加或编辑它的时间跨度。
我使用了datetimepicker,但我不知道如何在将其保存到数据库时将其转换为timespan。有人能帮我解决这个问题吗。
谢谢大家! 此解决方案绑定到设置模型时间跨度的DateTimeOffset属性
Index.razor
@page "/"
<input type="time" @bind-value="FromTime" />
<input type="time" @bind-value="ToTime" />
<hr />
@ConvertTo12HourFormat(SomeModel.FromTime) - @ConvertTo12HourFormat(SomeModel.ToTime)
SomeModel.cs
using System;
using Microsoft.AspNetCore.Components;
public partial class Index : ComponentBase
{
readonly DateTimeOffset aDate = new DateTime(2020, 01, 01);
public SomeModel SomeModel { get; set; } = new SomeModel();
public DateTimeOffset FromTime
{
get { return aDate.Add(SomeModel.FromTime); }
set { SomeModel.FromTime = value.Subtract(aDate); }
}
public DateTimeOffset ToTime
{
get { return aDate.Add(SomeModel.ToTime); }
set { SomeModel.ToTime = value.Subtract(aDate); }
}
public string ConvertTo12HourFormat(TimeSpan time) => aDate.Add(time).ToString("hh:mm:ss tt");
}
public class SomeModel
{
public short TimeSlotID { get; set; }
public TimeSpan FromTime { get; set; }
public TimeSpan ToTime { get; set; }
}
时间跨度是一段时间。日期时间是时间的一瞬间。你确定你想要一个时间段从4小时到8小时,而不是从下午4点到晚上8点?我想要12小时的格式。哇。。。谢谢@BrianParker:)您好,在razor页面中使用datetimepicker并在将该值插入数据库时将其转换为timespan是否可能?您好@Brian Parker,我收到错误消息。值为零且不保存为database@Aweelmarchons这是另一个问题,请将其作为另一个问题详细发布。好的@Brian Parker谢谢
@page "/"
<input type="time" @bind-value="FromTime" />
<input type="time" @bind-value="ToTime" />
<hr />
@ConvertTo12HourFormat(SomeModel.FromTime) - @ConvertTo12HourFormat(SomeModel.ToTime)
using System;
using Microsoft.AspNetCore.Components;
public partial class Index : ComponentBase
{
readonly DateTimeOffset aDate = new DateTime(2020, 01, 01);
public SomeModel SomeModel { get; set; } = new SomeModel();
public DateTimeOffset FromTime
{
get { return aDate.Add(SomeModel.FromTime); }
set { SomeModel.FromTime = value.Subtract(aDate); }
}
public DateTimeOffset ToTime
{
get { return aDate.Add(SomeModel.ToTime); }
set { SomeModel.ToTime = value.Subtract(aDate); }
}
public string ConvertTo12HourFormat(TimeSpan time) => aDate.Add(time).ToString("hh:mm:ss tt");
}
public class SomeModel
{
public short TimeSlotID { get; set; }
public TimeSpan FromTime { get; set; }
public TimeSpan ToTime { get; set; }
}