我想用C#创建一个程序,用于计算两个"定制用户之间的时间跨度"日期。我写了一段代码,但它没有用。请帮助。
string a, b, c, d, e, f;
a = Console.ReadLine();
b = Console.ReadLine();
c = Console.ReadLine();
d = Console.ReadLine();
e = Console.ReadLine();
f = Console.ReadLine();
int g, h, i, j, k, l;
g = int.Parse(a);
h = int.Parse(b);
i = int.Parse(c);
j = int.Parse(d);
k = int.Parse(e);
l = int.Parse(f);
DateTime FirstDate = DateTime.Parse("g/h/i");
DateTime SecondDate = DateTime.Parse("j/k/l");
TimeSpan Difference = SecondDate - FirstDate;
Console.WriteLine(Difference.ToString());
Console.ReadLine();
答案 0 :(得分:0)
我不明白你在那里做了什么,但看看这个:
DateTime dt1 = new DateTime(2014,10,10,17,10,00); // 2014-10-10 17:10.00
DateTime dt2 = new DateTime(2014,10,10,13,05,00); // 2014-10-10 13:05.00
var difference = dt1.Subtract(dt2); // 04:05.00
答案 1 :(得分:0)
如果两者都是日期时间格式
,这将有效(SecondDate - FirstDate).TotalDays
//return float
或
(SecondDate - FirstDate).Days
//returns int
答案 2 :(得分:0)
如果您已经强制用户输入单独的日/月/年,则不应将它们连接成单个字符串,然后DateTime.Parse
它们 - 您将面临各种日期格式问题。相反,请单独使用它们:
using System;
public class Program
{
public static void Main()
{
string day1 = Console.ReadLine();
string month1 = Console.ReadLine();
string year1 = Console.ReadLine();
string day2 = Console.ReadLine();
string month2 = Console.ReadLine();
string year2 = Console.ReadLine();
DateTime date1 = new DateTime(year: int.Parse(year1), month: int.Parse(month1), day: int.Parse(day1));
DateTime date2 = new DateTime(year: int.Parse(year2), month: int.Parse(month2), day: int.Parse(day2));
TimeSpan ts = date2 - date1;
Console.WriteLine("There are {0} days(s) or {1} hour(s) or {2} minute(s) between those dates", ts.TotalDays, ts.TotalHours, ts.TotalMinutes );
}
}
或者,您可以让用户输入完整的日期,并解析这些日期:
(...)
string date1Str = Console.ReadLine();
string date1Str = Console.ReadLine();
DateTime date1 = DateTime.Parse(date1Str);
DateTime date2 = DateTime.Parse(date2Str);
(...)
答案 3 :(得分:-1)
我认为你要做的是:
using System;
public class Program
{
public static void Main()
{
string month1 = Console.ReadLine();
string day1 = Console.ReadLine();
string year1 = Console.ReadLine();
string month2 = Console.ReadLine();
string day2 = Console.ReadLine();
string year2 = Console.ReadLine();
DateTime date1 = DateTime.Parse(string.Format("{0}/{1}/{2}", month1, day1, year1));
DateTime date2 = DateTime.Parse(string.Format("{0}/{1}/{2}", month2, day2, year2));
TimeSpan ts = date2 - date1;
Console.WriteLine("There are {0} days(s) or {1} hour(s) or {2} minute(s) between those dates", ts.TotalDays, ts.TotalHours, ts.TotalMinutes );
}
}
你可以看到这个程序的live action fiddle!
这假设您根据月,日和年份阅读日期。如果你需要不同的格式,那么你需要重新排列一些东西,但这应该给你一般的想法。
作为旁注,如果你的程序有描述性的变量名,程序员到处都会很感激。调用变量a
或b
或c
是不好的做法。生产环境程序中的那种东西会让其他程序员快速踢出你的屁股!
编辑:
由于您输入的是日/月/年格式,请改用:
using System;
public class Program
{
public static void Main()
{
string day1 = Console.ReadLine();
string month1 = Console.ReadLine();
string year1 = Console.ReadLine();
string day2 = Console.ReadLine();
string month2 = Console.ReadLine();
string year2 = Console.ReadLine();
DateTime date1 = DateTime.Parse(string.Format("{0}/{1}/{2}", month1, day1, year1));
DateTime date2 = DateTime.Parse(string.Format("{0}/{1}/{2}", month2, day2, year2));
TimeSpan ts = date2 - date1;
Console.WriteLine("There are {0} days(s) or {1} hour(s) or {2} minute(s) between those dates", ts.TotalDays, ts.TotalHours, ts.TotalMinutes );
}
}