【发布时间】:2016-06-26 13:33:44
【问题描述】:
在我的 MongoDB 数据库中,有一个名为 BusSchedule 的集合,其中包含以下字段
公交时刻表
- -Double StartTime --> 包含整数小时和小数小时
- -Double EndTime --> 包含整数小时和小数小时
- -字符串 12hourFormatStartTime
- -字符串 12hourFormatEndTime
以下是一些可能存在的典型值
StartTime = 6.5 --> contains whole and fractional hours
EndTime = 9.5 --> contains whole and fractional hours
12hourFormatStartTime = "06:30 AM"
12hourFormatEndTime = "09:30 AM"
StartTime = 8.5 --> contains whole and fractional hours
EndTime = 14.25 --> contains whole and fractional hours
12hourFormatStartTime = "08:30 AM"
12hourFormatEndTime = "02:15 PM"
我想知道微软是否有某种现有的库,可以轻松地将整数小时和小数小时的双精度数据类型表示转换为 12 小时时钟格式时间。
StartTime = 8.5 -------translated to-----> 12hourFormatStartTime = "08:30 AM"
EndTime = 14.25 -------translated to-----> 12hourFormatEndTime = "02:15 PM"
StartTime = 6.5 -------translated to-----> 12hourFormatStartTime = "06:30 AM"
EndTime = 9.5 -------translated to-----> 12hourFormatEndTime = "09:30 AM"
如果我可以使用 C#,我会更喜欢。我试着做:
Double wholeAndFractionalHours = 14.25;
TimeSpan.FromHours(wholeAndFractionalHours).ToString(@"hh:mm tt"))
但它会抛出 System.FormatException “输入字符串的格式不正确。”
我不想浪费时间编写代码来进行上述翻译。有人可以告诉我微软或某些第 3 方开源库是否会处理上述要求?
感谢@Edin:
这是我最终编写的代码:
Double wholeAndFractionalHours = 14.25;
String timeIn12hourClockFormat = (new DateTime().AddHours(wholeAndFractionalHours)).ToString("h:mm tt", CultureInfo.InvariantCulture);
【问题讨论】:
-
是哪一个? JavaScript? C#?这与 MongoDB 有什么关系?要求图书馆和工具是题外话。这也是非常容易编写的代码。
-
对不起,如果我可以使用 C#,我会更喜欢。我尝试执行 TimeSpan.FromHours(logEventOfInterest.StartTime).ToString(@"hh:mm tt")),但它抛出 System.FormatException “输入字符串格式不正确。”
-
嗯?基础数学呢? 0.5 of 60 = 30; 14 > 12 因此“PM”等等。每种语言都有一个时间实现,您可以提供“小时”和“分钟”。大多数人甚至认为 14 点对你来说是下午 2 点。
标签: javascript c# timespan time-format