![](/img/trans.png)
[英]Given a local timezone and a target timezone how can I find the target's current time C#
[英]How can I get the current lesson, given a collection of lesson time ranges in C#?
我正在為一所學校使用 Windows Forms 和 C# 制作時間表程序。
學校使用 API 返回包含課程列表的 JSON object。
在每節課的信息中提供了該課的時間段,格式為;
hh:mm - hh:mm
(24 小時格式)。
給定課時時間和當前時間,我需要找到當前課程。
提前致謝
因此,您的問題中有很多缺失的部分,但是如果您想以特定格式解析時間,則可以使用DateTime.ParseExact
。 下面是一個基於 24 小時制的示例:
// 24 hour clock. If this is a 12 hour clock, use the format "hh:mm"
var startTime = DateTime.ParseExact("08:44", "HH:mm", CultureInfo.InvariantCulture);
var endTime = DateTime.ParseExact("14:22", "HH:mm", CultureInfo.InvariantCulture);
var timeToTest = DateTime.Now;
if((timeToTest >= startTime) && (timeToTest <= endTime))
{
Console.WriteLine("We are currently in range.");
}
請記住, ParseExact
返回的DateTime
將是它生成的那一天,因為沒有使用日/月/年來計算它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.