投字符串为datetime [英] cast string to datetime

查看:169
本文介绍了投字符串为datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有asp.net日历中选择日期'selectedDate'和时间Piker选择的时间开始时间我想添加的2串1的startTime对象的startDateTime

I have asp.net Calendar to select the date 'selectedDate' and time Piker to select time 'starttime ' I am trying to add the 2 strings 1 startTime object 'startDateTime'

        string strDate = Calendar1.SelectedDate.ToShortDateString(); ;
        string startTime = txtb_endTimeManual.Text;
        DateTime startDateTime = Convert.ToDateTime(strDate + startTime);

错误信息
字符串未被识别为有效的DateTime。

error message String was not recognized as a valid DateTime.

推荐答案

您需要在两个字符串之间添加一个空格。如果strDate是2012年1月15日和的startTime是下午六点30分00秒然后连接两个字符串给你。1/15/20126:30:00 PM,所以格式为全部关闭。

You need to add a space between the two strings. If strDate is '1/15/2012' and startTime is '6:30:00 PM' then concatenating the two strings give you '1/15/20126:30:00 PM' so the format is all off.

DateTime startDateTime = Convert.ToDateTime(strDate + " " + startTime);

这篇关于投字符串为datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆