Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
dear sir,
how to convert string into date format?
string selected_date="23-March-2014";
how to convert this selected_date into date format...
plz help...
Posted

1. First, check that the date string is a valid one using tryparse;
2. Specify the desired date format;
3. then do the conversion.

C#
using System;

public class Program
{
    public static void Main()
    {
         string selected_date="23-March-2014";

          Console.WriteLine("The date string is {0}", selected_date);

          DateTime dt;

          if (DateTime.TryParse(selected_date, out dt))
          {
               string format = "dd-MMMM-yyyy";
               Console.WriteLine(dt.ToString(format));
          }
          else
          {
              Console.WriteLine("Not a valid datetime");
          }
    }
}
 
Share this answer
 
Comments
Rajkumar_007 25-Mar-14 1:44am    
Thank You sir :0
Further to solution 1.

You've tagged C# in your question so you can't use CDate(selected_date) - that is a VB function (there are ways of using VB.NET functions in C# but I'm not going to say how)

There are a few ways of converting strings to dates and some are better than others in certain circumstances.

Let's start with Convert.ToDateTime
DateTime d1 = Convert.ToDateTime(selected_date);
will work with the text you have in your example (which is one of the better ways of passing dates around)
But what if string selected_date="3/30/2014"; (i.e. in numeric US format). Where I currently am, that would result in a FormatException
Quote:
String was not recognized as a valid DateTime.
So I'm going to make sure that Visual Studio knows that I want to use American date formats today by using a CultureInfo[^]
IFormatProvider CultureToUse = new System.Globalization.CultureInfo("en-US", true);
DateTime d1 = Convert.ToDateTime(d, CultureToUse);

Another way of converting a string to a date is to use DateTime.Parse. I actually prefer this to using Convert but that is mainly a subjective view. You can use CultureInfo with Parse too.
SQL
DateTime d2 = DateTime.Parse(selected_date);
DateTime d3 = DateTime.Parse(selected_date, CultureToUse);


Finally, my preferred method is to use DateTime.TryParse. Why preferred? Because it won't throw an exception, it returns true if it was able to convert the string to a date, and false if it couldn't. And yes, you can use CultureInfo with this too.
C#
DateTime d4, d5;
Boolean b = DateTime.TryParse(selected_date, out d4);
Boolean b1 = DateTime.TryParse(selected_date, CultureToUse, System.Globalization.DateTimeStyles.None, out d5);

More info on TryParse[^] can be found on this link.
 
Share this answer
 
C#
string myDate = "21-07-2006";
DateTime myDateTime = DateTime.Parse(myDate);


myDate = DateTime.ParseExact(value, "MM/yy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);



[Edit member="Tadit"]
Added pre tags.
[/Edit]
 
Share this answer
 
v2
The simplest way
DateTime dt = (DateTime)Convert.ChangeType("stringWhichreRresentDateTime", typeof(DateTime));
 
Share this answer
 
You can use these methods for string to date conversion
VB
CDate(selected_date)

or
C#
Convert.ToDateTime(selected_date)


[Edit member="Tadit"]
Added pre tags.
[/Edit]
 
Share this answer
 
v3
Here are useful date formats
ASP DATES

string selected_date="23-March-2014";

C#
String selected_date= DateTime.Now.ToString("dd-MMMM-yyyy");


This code will return current date with your desired format 23-March-2014

hope this helps
 
Share this answer
 
Comments
CHill60 24-Mar-14 8:50am    
No it won't - it will return a String which the OP already has. They want a date - "how to convert this selected_date into date format..."
C#
Convert.ToDateTime(TxtBirthDate.Text);


[Edit member="Tadit"]
Added pre tags.
[/Edit]
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900