c# - 根据到达时间和旅行时间计算发射时间

标签 c# datetime

我需要根据给定的到达时间和旅行时间计算发射时间。我已经研究过 DateTime,但我不太确定我会怎么做。我正在使用 monthCalander 获取以下格式的到达日期时间。

Example:

Arrival_time = 20/03/2013 09:00:00
Travel_time = 00:30:00

Launch_time = Arrival_time - Travel_time

Launch_time should equal: 20/03/2013 08:30:00

有人可以告诉我一个简单的方法来实现这个目标吗?非常感谢。

最佳答案

使用TimeSpan :

DateTime arrivalTime = new DateTime(2013, 03, 20, 09, 00, 00);
// Or perhaps: DateTime arrivalTime = monthCalendar.SelectionStart;

TimeSpan travelTime = TimeSpan.FromMinutes(30);
DateTime launchTime = arrivalTime - travelTime;

如果出于某种原因您不能使用 MonthCalendar.SelectionStart 获取 DateTime 并且您只有可用的字符串,您可以按如下方式将其解析为 DateTime(针对特定格式) :

string textArrivalTime = "20/03/2013 09:00:00";
string dateTimeFormat = "dd/MM/yyyy HH:mm:ss";

DateTime arrivalTime = DateTime.ParseExact(textArrivalTime, dateTimeFormat, CultureInfo.InvariantCulture);

关于c# - 根据到达时间和旅行时间计算发射时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15465320/

相关文章:

objective-c - NSDateFormatter 生产(空)SQLite 和 iOS 5.1

php - 如何在 PHP 中使用 BRT 时区缩写解析字符串

c# - 如何实现 git id 或构建到 c# 应用程序

c# - 为什么 C# 和 C++ 使用 _<variableName> 编码约定?

c# - 使用 q 退出程序

C#/Mono 在后台运行服务器

javascript - 在 momentjs 中将分钟添加到日期时间

c# - 有没有办法以编程方式将文件从 TrueCrypt 磁盘读入内存?

datetime - Symfony2 : How to operate with datetime-objects

mysql - 如何仅使用日期从 DATETIME 列中进行选择?