将Srting转换为日期格式。 [英] Convert Srting to date format.
问题描述
我有两个文本框,一个包含数据库中的日期,另一个是将时间附加到其中。
I have two textboxes one contains date from database and another is to append time into it.
TextBox Date = new TextBox();
Date.Text = "11/05/2015"; //dd/MM/yyyy
TextBox txt1 = new TextBox();
txt1.Text = "12:00:00";
我已连接两个文本框以生成标准日期时间字符串,如下所示。
I have Concatenated the two textboxes to generate standard datetime string as follows.
var str1 = Date.Text+'.'+txt1.Text;
我已经检查过sting返回相同的值,即11/05 / 2015 12:00:00
现在我将字符串值转换为日期时间。
我这样做。
and I have checked that sting is returning the same value i.e. "11/05/2015 12:00:00"
Now I'm converting the string value to datetime.
I'm doing like this.
var d1 = DateTime.ParseExact(str1, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
var t1 = d1.ToString("MM/dd/yyyy HH:mm:ss");
但每次显示错误string is未被识别为有效的DateTime。
But every time it shows the error "string was not recognized as a valid DateTime".
推荐答案
这适用:
This works:
string str = "11/05/2015 12:00:00";
var d1 = DateTime.ParseExact(str, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
这也有效:
This works as well:
string str = "11/05/2015 12:00:00";
var d1 = DateTime.ParseExact(str, "dd/MM/yyyy HH:mm:ss",
null);
您可以在这里测试它们:
http://csharppad.com / [ ^ ]
更改这:
Change this:
var str1 = Date.Text+' '+txt1.Text;
这篇关于将Srting转换为日期格式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!