Click here to Skip to main content
Click here to Skip to main content

Convert DateTime string to DateTime variable

, 12 Dec 2012
Rate this:
Please Sign up or sign in to vote.
Convert DateTime string to DateTime variable

Original post located at :  Convert DateTime string to DateTime variable 

In this post I am going to discuss about Conversion of String data in to DateTime.

Problem
For most of the beginner developer they face the problem when "Inserting or Updating" DateTime value in "Database" which is having different presentation of DateTime value than DateTime string entered in UI of Application. For Example DatTime input accept date in format "DD/MM/YYYY" i.e "26/06/2013" so when try to insert this type of DateTime data in DateTime column in database it cause problem and not allow to insert data in Database.

Solution
So to Avoid problem related to during database operation its better to convert DateTime string in DateTime variable i.e. convert entered datetime string value in datetime value. So to do it two thing require to do
1. its better to check in code that string is proper i.e. string is valid Datetime string or not.
2. And convert string of datetime in Database acceptable format to avoid error.
To achieve this in .net provide function called TryParseExact which allows to check the datetime string is valid or not and convertable to DateTime variable.

Example:

string dateTimeString = "28/08/2012";
var date=DateTime.ParseExact(dateTimeString, "dd/MM/yyyy", null).ToString("MM/dd/yyyy");
In above example is conversion take place from "dd/MM/yyyy" to "MM/dd/yyyy".
Note :
Just confirm that the format of the string representation must match the specified format exactly.

Following is code taken from MSDN which shows how this function works for different format.
    string[] dates =
    {
      "31/12/2010",
      "12/31/2010",
      "1/1/2011",
      "1/12/2011",
      "12-12-2010",
      "12-Dec-10",
      "12-December-2010"
    };
    string[] formattedDates = new string[dates.Length];

    string[] formats = { "M/d/yyyy", "d/M/yyyy", "M-d-yyyy",
                        "d-M-yyyy", "d-MMM-yy", "d-MMMM-yyyy", };
    for (int i = 0; i < dates.Length; i++)
    {
      DateTime date;
      if (DateTime.TryParseExact(dates[i], formats,
                                CultureInfo.InvariantCulture,
                                 DateTimeStyles.None, out date))
        formattedDates[i] = date.ToString("dd/MM/yyyy");
    }

You take reference of MSDN for creating custom format string. - Custom Date and Time Format Strings
Conclusion
So by using this function you avoid error of conversion of DateTime string at run-time easily.
Reference : DateTime.TryParseExact

Leave your comments if you like it.

License

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

Share

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

 
Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.
 
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
 

You can visit my blog

StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:


Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralMy vote of 3 PinmemberKlaus Luedenscheidt13-Dec-12 19:08 
QuestionLocal or UTC / Issue with multiple formats [modified] PinmemberRobert Slaney13-Dec-12 18:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 13 Dec 2012
Article Copyright 2012 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid