tags:

views:

48

answers:

3

Hai i am getting a date input(Not the DateTime.Now) from the user and i want that to transform and show in a Date format. E.g: in the XAML, will be specified and i want that to be shown as "PrintedOn - October 24,2008" .The string formatting is string.format("{0} - {1}",userText, userDate). Where am i going wrong?How can i convert the UserDate to the formatting i need. Please pecify the code. Thanks

A: 

if you have a date from the user:

just use DateTime.ToString()

If you need to get a date from a string, just use

DateTime. Parse(datestring)
jle
But i am getting the input from the user as a string(XAML).So how can i convert it to Date?
Kaja
A: 

From your code sample, I assume this is C# or VB.NET.

String.Format("{0} - {1}", userText, userDate.ToString("D"))

does something along those lines, and gives you a string in the current locale as a bonus. See the possible format strings for more info.

Thomas
+1  A: 

Use this :

String.Format("{0} - {1}", userText, Date.Parse(userDate).ToString("MMMM dd,yyyy"))

where userDate is currently your date as an unspecified format in a string from the XAML. If you know the format it's arriving in, use ParseExact instead.

CodeByMoonlight
this should probably be `String.Format("{0} - {1}", userText, (Convert.ToDateTime(userDate)).ToString("MMMM dd,yyyy"))` to correctly answer the question
Jim Schubert
Corrected the format now - oopsie
CodeByMoonlight
Hai,I think ur answer will work.But i am getting error.So i checked and i found that even the following code is not running.Any ideas?DateTime dateValue;string dateString = "2/16/2008 12:15:12 PM";dateValue = DateTime.Parse(dateString);when i debug, i cant pass the 3rd line..I am getting this error"String was not recognized as a valid DateTime"
Kaja
Hai its working after adding this code Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");Thanks all..
Kaja