468,765 Members | 1,181 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

DateTime.Parse()

js
I have a textbox contains text in the format of "yyyy/MM/dd hh:mm:ss".
I need to parse the text using System.DateTime.Parse() function with
custom format. I got an error using the following code. Could someone
help me with the customization? Thanks.

String was not recognized as a valid DateTime. at
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles) at System.DateTime.Parse(String s,
IFormatProvider provider, DateTimeStyles styles)

***** My code ****
CultureInfo format = (CultureInfo)
Thread.CurrentThread.CurrentCulture.Clone();
format.DateTimeFormat.Calendar YearMonthPattern = "yyyy'/'MM'";
format.DateTimeFormat.MonthDayPattern = "MM'/'dd";
this.calDate.SelectedDate = System.DateTime.Parse(this.txtDate.Text,
format, System.Globalization.DateTimeStyles.NoCurrentDateD efault);

Oct 3 '06 #1
5 4818
This isn't what you asked for, but a quick way is to use a cultureinfo of a
culture that uses your source format. In this case, Japanese looks right.

Here's the hack that I came up with, just in case you don't have time to
wait for a real solution.

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo MyCultureInfo =
new System.Globalization.CultureInfo("ja-JP");
string MyString = TextBox1.Text; // e.g. "2006/10/02 11:59:59";
DateTime MyDateTime = DateTime.Parse(MyString,
MyCultureInfo );
Label1.Text=MyDateTime.ToString();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Hack to parse a datetime string</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="TextBox1" runat="server">2006/10/02
11:59:59</asp:textbox><br />
<br />
<asp:button id="Button1" runat="server" text="Button"
onclick="Button1_Click" />
<br />
<br />
<asp:label id="Label1" runat="server"
text="Label"></asp:label></div>
</form>
</body>
</html>

"js" <an********@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>I have a textbox contains text in the format of "yyyy/MM/dd hh:mm:ss".
I need to parse the text using System.DateTime.Parse() function with
custom format. I got an error using the following code. Could someone
help me with the customization? Thanks.

String was not recognized as a valid DateTime. at
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles) at System.DateTime.Parse(String s,
IFormatProvider provider, DateTimeStyles styles)

***** My code ****
CultureInfo format = (CultureInfo)
Thread.CurrentThread.CurrentCulture.Clone();
format.DateTimeFormat.Calendar YearMonthPattern = "yyyy'/'MM'";
format.DateTimeFormat.MonthDayPattern = "MM'/'dd";
this.calDate.SelectedDate = System.DateTime.Parse(this.txtDate.Text,
format, System.Globalization.DateTimeStyles.NoCurrentDateD efault);

Oct 3 '06 #2
js
Thanks. I am really looking for how to use a custom DateTimeformat,
instead of using a foreign cultureinfo that happens to macth my date
string format. I read the help on MSDN, but sitll could not figure out
how to use Format Pattern in the DateTimeFormatInfo class. Any idea?

Ken Cox [Microsoft MVP] wrote:
This isn't what you asked for, but a quick way is to use a cultureinfo of a
culture that uses your source format. In this case, Japanese looks right.

Here's the hack that I came up with, just in case you don't have time to
wait for a real solution.

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo MyCultureInfo =
new System.Globalization.CultureInfo("ja-JP");
string MyString = TextBox1.Text; // e.g. "2006/10/02 11:59:59";
DateTime MyDateTime = DateTime.Parse(MyString,
MyCultureInfo );
Label1.Text=MyDateTime.ToString();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Hack to parse a datetime string</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="TextBox1" runat="server">2006/10/02
11:59:59</asp:textbox><br />
<br />
<asp:button id="Button1" runat="server" text="Button"
onclick="Button1_Click" />
<br />
<br />
<asp:label id="Label1" runat="server"
text="Label"></asp:label></div>
</form>
</body>
</html>

"js" <an********@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
I have a textbox contains text in the format of "yyyy/MM/dd hh:mm:ss".
I need to parse the text using System.DateTime.Parse() function with
custom format. I got an error using the following code. Could someone
help me with the customization? Thanks.

String was not recognized as a valid DateTime. at
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles) at System.DateTime.Parse(String s,
IFormatProvider provider, DateTimeStyles styles)

***** My code ****
CultureInfo format = (CultureInfo)
Thread.CurrentThread.CurrentCulture.Clone();
format.DateTimeFormat.Calendar YearMonthPattern = "yyyy'/'MM'";
format.DateTimeFormat.MonthDayPattern = "MM'/'dd";
this.calDate.SelectedDate = System.DateTime.Parse(this.txtDate.Text,
format, System.Globalization.DateTimeStyles.NoCurrentDateD efault);
Oct 3 '06 #3
js
Thanks. I am really looking for how to use custom format, instead of
using a cultureinfo that macth the date string I have on hand. I read
the help on MSDN, but sitll could not figure out how to use Format
Pattern in the DateTimeFormatInfo class. Any idea?

Ken Cox [Microsoft MVP] wrote:
This isn't what you asked for, but a quick way is to use a cultureinfo of a
culture that uses your source format. In this case, Japanese looks right.

Here's the hack that I came up with, just in case you don't have time to
wait for a real solution.

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo MyCultureInfo =
new System.Globalization.CultureInfo("ja-JP");
string MyString = TextBox1.Text; // e.g. "2006/10/02 11:59:59";
DateTime MyDateTime = DateTime.Parse(MyString,
MyCultureInfo );
Label1.Text=MyDateTime.ToString();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Hack to parse a datetime string</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="TextBox1" runat="server">2006/10/02
11:59:59</asp:textbox><br />
<br />
<asp:button id="Button1" runat="server" text="Button"
onclick="Button1_Click" />
<br />
<br />
<asp:label id="Label1" runat="server"
text="Label"></asp:label></div>
</form>
</body>
</html>

"js" <an********@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
I have a textbox contains text in the format of "yyyy/MM/dd hh:mm:ss".
I need to parse the text using System.DateTime.Parse() function with
custom format. I got an error using the following code. Could someone
help me with the customization? Thanks.

String was not recognized as a valid DateTime. at
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles) at System.DateTime.Parse(String s,
IFormatProvider provider, DateTimeStyles styles)

***** My code ****
CultureInfo format = (CultureInfo)
Thread.CurrentThread.CurrentCulture.Clone();
format.DateTimeFormat.Calendar YearMonthPattern = "yyyy'/'MM'";
format.DateTimeFormat.MonthDayPattern = "MM'/'dd";
this.calDate.SelectedDate = System.DateTime.Parse(this.txtDate.Text,
format, System.Globalization.DateTimeStyles.NoCurrentDateD efault);
Oct 3 '06 #4
Here's another crack at it:

<%@ Page Language="C#" %>
<%@ import namespace="System.Globalization" %>
<%@ import namespace="System.Threading" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Button2_Click(object sender, EventArgs e)
{
CultureInfo fmt =
(CultureInfo)Thread.CurrentThread.CurrentCulture.C lone();
fmt.DateTimeFormat.YearMonthPattern = @"yyyy'/'MM'";
fmt.DateTimeFormat.MonthDayPattern = @"MM'/'dd";
fmt.DateTimeFormat.DateSeparator = @"/";
this.calDate.SelectedDate = System.DateTime.Parse(this.txtDate.Text,
fmt, System.Globalization.DateTimeStyles.NoCurrentDateD efault);
Label1.Text= System.DateTime.Parse(this.txtDate.Text, fmt,
System.Globalization.DateTimeStyles.NoCurrentDateD efault).ToString();
this.calDate.VisibleDate = System.DateTime.Parse(this.txtDate.Text,
fmt, System.Globalization.DateTimeStyles.NoCurrentDateD efault);
this.calDate.SelectedDayStyle.BackColor = System.Drawing.Color.Red;
this.calDate.TodaysDate = System.DateTime.Parse(this.txtDate.Text,
fmt, System.Globalization.DateTimeStyles.NoCurrentDateD efault);
this.calDate.SelectionMode = CalendarSelectionMode.Day;
}

protected void calDate_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date == calDate.VisibleDate)
{

e.Cell.ForeColor = System.Drawing.Color.Blue;

e.Cell.BackColor = System.Drawing.Color.Pink;

}

}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Hack to parse a datetime string</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="txtDate" runat="server">2006/10/02
11:59:59</asp:textbox><br />
<br />
<br />
<asp:button id="Button2" runat="server" onclick="Button2_Click"
text="Button" /><br />
<br />
<asp:label id="Label1" runat="server" text="Label"></asp:label><br
/>
<br />
<asp:calendar id="calDate" runat="server"
ondayrender="calDate_DayRender"></asp:calendar>
</div>
</form>
</body>
</html>

Ken
Microsoft MVP [ASP.NET]
"js" <an********@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Thanks. I am really looking for how to use a custom DateTimeformat,
instead of using a foreign cultureinfo that happens to macth my date
string format. I read the help on MSDN, but sitll could not figure out
how to use Format Pattern in the DateTimeFormatInfo class. Any idea?

Ken Cox [Microsoft MVP] wrote:
>This isn't what you asked for, but a quick way is to use a cultureinfo of
a
culture that uses your source format. In this case, Japanese looks right.

Here's the hack that I came up with, just in case you don't have time to
wait for a real solution.

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo MyCultureInfo =
new System.Globalization.CultureInfo("ja-JP");
string MyString = TextBox1.Text; // e.g. "2006/10/02 11:59:59";
DateTime MyDateTime = DateTime.Parse(MyString,
MyCultureInfo );
Label1.Text=MyDateTime.ToString();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Hack to parse a datetime string</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="TextBox1" runat="server">2006/10/02
11:59:59</asp:textbox><br />
<br />
<asp:button id="Button1" runat="server" text="Button"
onclick="Button1_Click" />
<br />
<br />
<asp:label id="Label1" runat="server"
text="Label"></asp:label></div>
</form>
</body>
</html>

"js" <an********@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googleg roups.com...
>I have a textbox contains text in the format of "yyyy/MM/dd hh:mm:ss".
I need to parse the text using System.DateTime.Parse() function with
custom format. I got an error using the following code. Could someone
help me with the customization? Thanks.

String was not recognized as a valid DateTime. at
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles) at System.DateTime.Parse(String s,
IFormatProvider provider, DateTimeStyles styles)

***** My code ****
CultureInfo format = (CultureInfo)
Thread.CurrentThread.CurrentCulture.Clone();
format.DateTimeFormat.Calendar YearMonthPattern = "yyyy'/'MM'";
format.DateTimeFormat.MonthDayPattern = "MM'/'dd";
this.calDate.SelectedDate = System.DateTime.Parse(this.txtDate.Text,
format, System.Globalization.DateTimeStyles.NoCurrentDateD efault);

Oct 4 '06 #5
js
Thank you, Ken. I tried your code and it did not work. I just
realized that there was a typo in my original date format. It acutally
is 2006:09:28:15:56:38 format. So it is yyyy:MM:dd:hh:mm:ss format. I
don't think there is any existing cultures with that format.

Ken Cox [Microsoft MVP] wrote:
Here's another crack at it:

<%@ Page Language="C#" %>
<%@ import namespace="System.Globalization" %>
<%@ import namespace="System.Threading" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Button2_Click(object sender, EventArgs e)
{
CultureInfo fmt =
(CultureInfo)Thread.CurrentThread.CurrentCulture.C lone();
fmt.DateTimeFormat.YearMonthPattern = @"yyyy'/'MM'";
fmt.DateTimeFormat.MonthDayPattern = @"MM'/'dd";
fmt.DateTimeFormat.DateSeparator = @"/";
this.calDate.SelectedDate = System.DateTime.Parse(this.txtDate.Text,
fmt, System.Globalization.DateTimeStyles.NoCurrentDateD efault);
Label1.Text= System.DateTime.Parse(this.txtDate.Text, fmt,
System.Globalization.DateTimeStyles.NoCurrentDateD efault).ToString();
this.calDate.VisibleDate = System.DateTime.Parse(this.txtDate.Text,
fmt, System.Globalization.DateTimeStyles.NoCurrentDateD efault);
this.calDate.SelectedDayStyle.BackColor = System.Drawing.Color.Red;
this.calDate.TodaysDate = System.DateTime.Parse(this.txtDate.Text,
fmt, System.Globalization.DateTimeStyles.NoCurrentDateD efault);
this.calDate.SelectionMode = CalendarSelectionMode.Day;
}

protected void calDate_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date == calDate.VisibleDate)
{

e.Cell.ForeColor = System.Drawing.Color.Blue;

e.Cell.BackColor = System.Drawing.Color.Pink;

}

}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Hack to parse a datetime string</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="txtDate" runat="server">2006/10/02
11:59:59</asp:textbox><br />
<br />
<br />
<asp:button id="Button2" runat="server" onclick="Button2_Click"
text="Button" /><br />
<br />
<asp:label id="Label1" runat="server" text="Label"></asp:label><br
/>
<br />
<asp:calendar id="calDate" runat="server"
ondayrender="calDate_DayRender"></asp:calendar>
</div>
</form>
</body>
</html>

Ken
Microsoft MVP [ASP.NET]
"js" <an********@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Thanks. I am really looking for how to use a custom DateTimeformat,
instead of using a foreign cultureinfo that happens to macth my date
string format. I read the help on MSDN, but sitll could not figure out
how to use Format Pattern in the DateTimeFormatInfo class. Any idea?

Ken Cox [Microsoft MVP] wrote:
This isn't what you asked for, but a quick way is to use a cultureinfo of
a
culture that uses your source format. In this case, Japanese looks right.

Here's the hack that I came up with, just in case you don't have time to
wait for a real solution.

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo MyCultureInfo =
new System.Globalization.CultureInfo("ja-JP");
string MyString = TextBox1.Text; // e.g. "2006/10/02 11:59:59";
DateTime MyDateTime = DateTime.Parse(MyString,
MyCultureInfo );
Label1.Text=MyDateTime.ToString();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Hack to parse a datetime string</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="TextBox1" runat="server">2006/10/02
11:59:59</asp:textbox><br />
<br />
<asp:button id="Button1" runat="server" text="Button"
onclick="Button1_Click" />
<br />
<br />
<asp:label id="Label1" runat="server"
text="Label"></asp:label></div>
</form>
</body>
</html>

"js" <an********@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
I have a textbox contains text in the format of "yyyy/MM/dd hh:mm:ss".
I need to parse the text using System.DateTime.Parse() function with
custom format. I got an error using the following code. Could someone
help me with the customization? Thanks.

String was not recognized as a valid DateTime. at
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles) at System.DateTime.Parse(String s,
IFormatProvider provider, DateTimeStyles styles)

***** My code ****
CultureInfo format = (CultureInfo)
Thread.CurrentThread.CurrentCulture.Clone();
format.DateTimeFormat.Calendar YearMonthPattern = "yyyy'/'MM'";
format.DateTimeFormat.MonthDayPattern = "MM'/'dd";
this.calDate.SelectedDate = System.DateTime.Parse(this.txtDate.Text,
format, System.Globalization.DateTimeStyles.NoCurrentDateD efault);
Oct 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by ChrisM | last post: by
4 posts views Thread by Hans Merkl | last post: by
6 posts views Thread by Ante Perkovic | last post: by
38 posts views Thread by nobody | last post: by
11 posts views Thread by Cor Ligthert | last post: by
4 posts views Thread by =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.