在c#中将整数转换为datetime [英] convert integer to datetime in c#
问题描述
Console.WriteLine("please enter start date");
List <int> start = new List <int>();
for (int i = 0; i <= 4; i++)
{
start.Add(i);
Console.ReadLine(); }
List <int> end = new List <int>();
for (int i = 0; i <= 4; i++)
{
end.Add(i);
Console.ReadLine(); }
// how to do that in the run time start result was "01.01.1990 " ,"01.01.1999" and end result was "02.01.1990","09.10.2013"
推荐答案
Console.WriteLine("please enter start date");
string sStartdate = string.Empty;
//Input date
sStartdate = Console.ReadLine();
DateTime dStartDate;
//Parse the input to date time
DateTime.TryParse(sStartdate, out dStartDate);
//Display it
Console.Write(dStartDate);
//Wait for enter key hit (to hang the console window for a input)
Console.ReadLine();
// for miltiple entry使用此解决方案------ ---------------------------------------------
列表< datetime> dtArray = new List< datetime>();
while(true)
{
Console.WriteLine (输入日期);
string sStartdate = string.Empty;
sStartdate = Console.ReadLine();
DateTime dStartDate;
if(DateTime.TryParse(sStartdate,out dStartDate))
{
dtArray.Add(dStartDate) ;
}
string sAns =Y;
Console.WriteLine(想要进入下一个(是/否)?);
do
{
sAns = Console.ReadLine() ;
if(sAns.ToUpper()!=Y&& sAns.ToUpper()!=N)
Console.WriteLine(无效输入,有效输入是Y / N?);
} while(sAns.ToUpper()!=Y&& sAns.ToUpper() !=N);
if(sAns.ToUpper()==N)
{
休息;
} < br $>
}
foreach(dtArray中的var项目)
{
控制台。 WriteLine(item);
}
Console.ReadLine();
// for miltiple entry use this solution ---------------------------------------------------
List<datetime> dtArray = new List<datetime>();
while (true)
{
Console.WriteLine("Enter date");
string sStartdate = string.Empty;
sStartdate = Console.ReadLine();
DateTime dStartDate;
if (DateTime.TryParse(sStartdate, out dStartDate))
{
dtArray.Add(dStartDate);
}
string sAns = "Y";
Console.WriteLine("Want to enter next (Y/N)?");
do
{
sAns = Console.ReadLine();
if (sAns.ToUpper() != "Y" && sAns.ToUpper() != "N")
Console.WriteLine("Invalid Input, Valid inputs are Y/N?");
} while (sAns.ToUpper() != "Y" && sAns.ToUpper() != "N");
if (sAns.ToUpper() == "N")
{
break;
}
}
foreach (var item in dtArray)
{
Console.WriteLine(item);
}
Console.ReadLine();
尝试采取字符串中的输入(使用Console.ReadLine)然后将其放入数组或列表中或直接转换为datetime(DateTime.TryParse)。
Try to take the input in a string (with Console.ReadLine) and then put it into array or list or direct convert to datetime (DateTime.TryParse).
试试这个
try this one
int hour = 23;
var today = DateTime.Today;
var time = new DateTime(today.Year, today.Month, today.Day, hour, 0, 0);
var time = DateTime.Today + TimeSpan.FromHours(hour);
这篇关于在c#中将整数转换为datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!