1

I have a form with few datetime pickers, to be exact two pairs of dates and times.

enter image description here

Data from those pickers is combined and saved to database as DateTime in format

 16/05/2012 11:28:00 a.m.

Now, what I want to do is get value from database and put it into datetime pickers.

I've tried something like

string plannedDate =(dbFunctions.SQLReadColumn("task", "PlannedDate", TaskID));
        DateTime pDate = new DateTime(plannedDate.Substring(0,9);
        dtpDatePicker.Value=pDate;

where plannedDate contains string data in format as mentioned above, but I can't convert it to DateTime (second line incorrect). Is there an easy way to do it or do I have to cut the string into pieces for year, month etc?

4

3 回答 3

5

这样做

dtpDatePicker.Value = Convert.ToDateTime(plannedDate.Substring(0, 10)); 

同样的时间

mytimepicker.Value = Convert.ToDateTime(plannedDate.Substring(10, 6));
于 2012-05-16T01:13:34.417 回答
3

我个人喜欢DateTime.TryParse 方法,在您可能收到无效字符串作为输入但不希望抛出异常的情况下:

DateTime dateValue;
if (DateTime.TryParse(dateString, out dateValue))
{
    // Conversion succedded
}
else
{
    // Conversion failed
}
于 2012-05-16T01:24:24.237 回答
0

尝试

dateTimePicker1.Value = Convert.ToDateTime( gridView1.GetRowCellValue(rowHandle, "CDATE").ToString());
于 2016-05-20T18:47:04.540 回答