Library code snippets

How to convert a string to a date

Finding out how to convert one type to another in .NET is tricky. Here is the answer to converting a string to a DateTime type. Notice that any of the string formats work -- how nice!

//string strDate="2002-10-03";
//string strDate="10/04/2002";
string strDate="10/05";
DateTime dt = (DateTime)(TypeDescriptor.GetConverter(new DateTime(1990,5,6)).ConvertFrom(strDate));
Response.Write("<hr>" + dt.DayOfWeek);

Comments

  1. 13 Sep 2007 at 08:14

    'Converts the date in format dd/MM/yyyy to date format in MM/dd/yyyy

    Dim stDate As String = "31/12/2006"

    Dim dtDate As Date

    dtDate = DateTime.Parse(stDate, Globalization.CultureInfo.CreateSpecificCulture(

    "en-CA"))

    MsgBox(dtDate)

  2. 17 Jun 2005 at 02:06

    Hey, to ALL Developers and Programmers,

     

      Here's a simple and easy way of solving all your Date Programming and Conversion  problems.


    Developer's Dateware offers a much greater variety of date features, versus what is offered in typical programming environments.  You will not need to fuss with the PC's Locale system any longer – these features will do it for you.  These Date functions range from general areas to specific areas.


    Developer's Dateware offers: 
       1)  simplicity in performing any kind of Date Operation.
       2)  much more capabilties,  for a lot less money.
       3)  Excellent
    Results and Performance are Guaranteed .
                   And yet, we’ll beat the rest! 
       4)   supports many different date formats... 

    Are you ready to take a challange for something new and much better?

     

    DateFormats,      and yes, I’m still around…

     

    The software is written in MSVC++ v6.0,  (with NO MFC Code).

     

    For Further Details about Developer's Dateware Software and demo,

            contact Thad at:  auroraprojectusaf@yahoo.com

       --6/22/2008--

  3. 14 Jun 2005 at 13:22

    Svy,
    Cheers mate that one had thus far eluded me.

  4. 19 Apr 2005 at 10:41
    I've tried this piece of code:

    DateTime startTime;

    start = Convert.ToDateTime(startTimePicker.Text);

    Console.WriteLine(startTime.ToLongTimeString());

    The problem with this is that the results are string values but i want the actual time value in order to be able to use it to compare times to prevent overlaps in meeting times.
  5. 22 Dec 2003 at 06:18

    How can i get just date part (22/12/2003) out of datetime(22/12/2003/ 1:23:00 PM) without convert to string datetypes (ShortDateString etc).
    for instance if ur passing datetime parameters to stored procedures that perform search based on dates supplied.
    Passing dates with time would not show last record coz it compares with time too.


  6. 01 Dec 2003 at 11:40
    'Make sure that your date is correct, if you try for example to do "11/31/03"
    'you will get an error because november only has 30 days to it the correct string
    'is "11/30/2003"

    dim dateString as string = "11/30/2003"

    dim convertedDate as Date = CDate(dateString)
  7. 16 Apr 2003 at 11:54

    Another Way is Also
    DateTime dt = DateTime.Parse(strdate);



  8. 28 Feb 2003 at 14:19

    DateTime dtDate = Convert.ToDateTime(strDate);


    "Convert" is a class in a "System" namespace.

  9. 01 Jan 1999 at 00:00

    This thread is for discussions of How to convert a string to a date.

Leave a comment

Sign in or Join us (it's free).

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

We'd love to hear what you think! Submit ideas or give us feedback