PC Review


Reply
Thread Tools Rate Thread

DateTime.Parse()

 
 
js
Guest
Posts: n/a
 
      3rd Oct 2006
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);

 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      3rd Oct 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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);
>



 
Reply With Quote
 
js
Guest
Posts: n/a
 
      3rd Oct 2006
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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);
> >


 
Reply With Quote
 
js
Guest
Posts: n/a
 
      3rd Oct 2006
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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);
> >


 
Reply With Quote
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      4th Oct 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> >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);
>> >

>



 
Reply With Quote
 
js
Guest
Posts: n/a
 
      4th Oct 2006
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.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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >> >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);
> >> >

> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DateTime.Parse() vs DateTime.ParseExact() Peter Duniho Microsoft C# .NET 3 19th Jun 2007 03:19 PM
Weird CultureInfo - DateTime.Parse() and Decimal.Parse() Rico Microsoft C# .NET 8 22nd Sep 2006 10:59 AM
DateTime parse ahager via DotNetMonster.com Microsoft ADO .NET 2 4th Oct 2005 03:14 AM
Parse datetime =?Utf-8?B?ZHdpZ2h0?= Microsoft C# .NET 4 18th May 2005 02:24 PM
DateTime.Parse .. what's different in .NET 1.1 Atul Agarwal Microsoft Dot NET Framework 4 27th Aug 2003 07:23 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:57 AM.