473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 5116
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: ChrisM | last post by:
Could anyone please tell me the difference between these 2 lines: myDate = DateTime.Now; and myDate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy")); Seeing as later on in the...
1
by: vooose | last post by:
Consider an application which has *many* references to DateTime.Parse(string); Is there a way, say when the application first starts up to force this method to use a specific IFormatProvider,...
4
by: Hans Merkl | last post by:
Does anybody know of a library that can handle strings pf various formats and conver them to a DateTime value? The strings are coming from a webform and I can't restrict the input (it's not my...
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
11
by: Cor Ligthert | last post by:
Hello everybody, Jay and Herfried are telling me every time when I use CDate that using the datetime.parseexact is always the best way to do String to datetime conversions. They don't tell why...
11
by: Tim | last post by:
Hi, I am trying to do something simple. Convert a string date to datetime but it is not working and is giving me a baffling error! System.Convert.ToDateTime("Jan 30, 2006")...
4
by: tshad | last post by:
Is there any difference between convert.ToDateTime and System.DateTime.Parse? I am using them both and they seem the same. Is one better to use than another? Thanks, Tom
4
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I am having a problem formatting a string when the time is in format hh.mm.ss - used in Europe Parse seems ok when the date uses "/" or "." as seperator but I get an exception when time...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.