DateTime.Parse()


J

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.NoCurrentDateDefault);
 
Ad

Advertisements

K

Ken Cox [Microsoft MVP]

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>
 
J

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?
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 said:
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.NoCurrentDateDefault);
 
J

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?
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 said:
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.NoCurrentDateDefault);
 
K

Ken Cox [Microsoft MVP]

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.Clone();
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.NoCurrentDateDefault);
Label1.Text= System.DateTime.Parse(this.txtDate.Text, fmt,
System.Globalization.DateTimeStyles.NoCurrentDateDefault).ToString();
this.calDate.VisibleDate = System.DateTime.Parse(this.txtDate.Text,
fmt, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
this.calDate.SelectedDayStyle.BackColor = System.Drawing.Color.Red;
this.calDate.TodaysDate = System.DateTime.Parse(this.txtDate.Text,
fmt, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
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 said:
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?
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 said:
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.NoCurrentDateDefault);
 
Ad

Advertisements

J

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.
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.Clone();
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.NoCurrentDateDefault);
Label1.Text= System.DateTime.Parse(this.txtDate.Text, fmt,
System.Globalization.DateTimeStyles.NoCurrentDateDefault).ToString();
this.calDate.VisibleDate = System.DateTime.Parse(this.txtDate.Text,
fmt, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
this.calDate.SelectedDayStyle.BackColor = System.Drawing.Color.Red;
this.calDate.TodaysDate = System.DateTime.Parse(this.txtDate.Text,
fmt, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
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 said:
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?
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>

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.NoCurrentDateDefault);
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top