Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单独存储日期时间值_C#_Datetime_Devexpress_Datetime Format - Fatal编程技术网

C# 单独存储日期时间值

C# 单独存储日期时间值,c#,datetime,devexpress,datetime-format,C#,Datetime,Devexpress,Datetime Format,我有两个变量,一个用于日期,另一个用于时间,我想将它们的值(日期+时间)存储到一个类型为DateTime的变量中 DateTime x; x = timeEdit1.Time; // return datetime as current time and current date x = dateEdit2.DateTime; // return datetime as current date and '12:00:00 AM' 您可以使用DateTime:和的相应属性: 如果要

我有两个变量,一个用于日期,另一个用于时间,我想将它们的值(日期+时间)存储到一个类型为
DateTime
的变量中

DateTime x;
x = timeEdit1.Time;      // return datetime as current time and current date
x = dateEdit2.DateTime;  // return datetime as current date and '12:00:00 AM'

您可以使用
DateTime
:和的相应属性:

如果要将它们存储在一个
DateTime

DateTime dateAndTime = dateEdit2.Date + timeEdit1.Time.TimeOfDay;

现在,您将这两个代码集于一身,您可以按照我的第一个代码片段所示提取它们。

您只需在初始化阶段添加日期和时间:

DateTime x = new DateTime(dateEdit2.Date.Year, dateEdit2.Date.Month, dateEdit2.Date.Day, timeEdit1.TimeOfDay.Hours, timeEdit2.TimeOfDay.Minutes, timeEdit2.TimeOfDay.Seconds);
要获取值并将其显示在MessageBox中,您只需执行以下操作:

MessageBox.Show(x.Date.ToString() + " " + x.TimeOfDay.ToSTring());

是的,但我不能将它们作为一个存储在DateTime中value@abdullahRasheed:然后按上图所示添加和提取它们,我已对其进行了编辑。另一方面,您应该使
timeEdit1.Time
a
TimeSpan
而不是
DateTime
。我得到错误消息:运算符“+”不能应用于操作数类型(时间跨度+DateTime@abdullahRasheed:您需要按照Tim显示的顺序将
TimeSpan
转换为
DateTime
DateTime
+
TimeSpan
@abdullahRasheed:我假设您使用了
timeEdit1.Time
而不是
timeEdit1.Time.TimeOfDay
。我已经在之后直接编辑了我的答案r我已经发布了。这就是为什么我建议您首先应该使用
TimeSpan
。指定问题的具体原因将有助于人们帮助您。
MessageBox.Show(x.Date.ToString() + " " + x.TimeOfDay.ToSTring());