Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] Convert String to DateTime

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    169

    Resolved [RESOLVED] [2.0] Convert String to DateTime

    I have a string with the month and year, separated by a "/". I need to convert this to a DateTime, but then convert it back to a string (don't ask, it's how the original developer made it). I have seen several examples on how to convert strings to DateTime, but none would work in this instance.

    Code:
    String date = "11/10";
    DateTime dt = Convert.ToDateTime(date);
    
    String result = dt.Month + "/" + dt.Year
    This would return "11/08", I guess it thinks the 10 is the day instead of the year. How can I get this to work in my instance?

  2. #2
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: [2.0] Convert String to DateTime

    A date requires a year, month, and day. If you don't have all 3 parts, you can't make a date. I guess you could always just hard-code 1 in for the day:

    Code:
    String date = "11/10".Replace("/", "/1/");
    DateTime dt = DateTime.Parse(date);
    result = string.Format("{0}/{1}", dt.Month, dt.Year);
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    169

    Re: [2.0] Convert String to DateTime

    Yes, that does work. Thank you.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2.0] Convert String to DateTime

    This would be the "proper" way:
    CSharp Code:
    1. string s = "11/10";
    2. DateTime d;
    3.  
    4. if (DateTime.TryParseExact(s,
    5.                            "MM/yy",
    6.                            null,
    7.                            System.Globalization.DateTimeStyles.None,
    8.                            out d))
    9.  
    10. {
    11.     MessageBox.Show(d.ToShortDateString());
    12. }
    13. else
    14. {
    15.     MessageBox.Show("Invalid date string.");
    16. }
    If you want to you can call ToString instead of ToShortDateString and pass any format string you like, e.g.
    CSharp Code:
    1. MessageBox.Show(d.ToString("MM/yy"));
    Note also that you should only use "MM/yy" if all single digit months are required to be padded with a leading zero. If they don't then you should use "M/yy", which will work for single or double digit months.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width