Results 1 to 3 of 3

Thread: [RESOLVED] [2005] Date to String formatting

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    UK
    Posts
    96

    Resolved [RESOLVED] [2005] Date to String formatting

    Hi

    I'm using DateTime.TryParseExact to verify dates that users put into a textbox (I know datepickers are better, but my users refuse to use them!).

    In one particular instance, I have a start date text box, an end date text box and a duration (months) numeric field. When the user has entered a start date and a duration, I want to be able to calculate the end date.

    I am using this code:

    Code:
    Dim tempStartDate As Date
            Dim tempEndDate As Date
            Dim okDate As Boolean
           
            okDate = DateTime.TryParseExact(txtRGStartDt.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, tempStartDate)
    
            tempEndDate = DateAdd(DateInterval.Month, nuDuration.Value, tempStartDate.Date)
    
            txtRGEndDt.Text  = tempEndDate.ToString("dd\MM\yyyy")
    The trouble is, the end date text is coming out all screwy e.g. if start date is 15/05/2005 and duration (months) is set to 1, end date is being set as '15M6y2005'.

    I think it's to do with the fact that even though I am specifying a format of 'dd/MM/yyyy' for the TryParseExact function, the date it returns (tempStartDate) is in the format 'MM/dd/yyyy', so when I use the toString function and specify the 'dd/MM/yyyy' format, it doesn't like it!

    Please does anyone know how to resolve this? Thanks.

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

    Re: [2005] Date to String formatting

    You've got your delimiters the wrong way around so they're actually escaping the following character. This:
    VB Code:
    1. tempEndDate.ToString("dd\MM\yyyy")
    should be this:
    VB Code:
    1. tempEndDate.ToString("dd/MM/yyyy")
    or, if you actually do want backslashes it should be this:
    VB Code:
    1. tempEndDate.ToString("dd\\MM\\yyyy")
    Also, use the members of the Date object itself instead of DateAdd, e.g.
    VB Code:
    1. tempEndDate = tempStartDate.Date.AddMonths(Convert.ToInt32(nuDuration.Value))
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    UK
    Posts
    96

    Re: [2005] Date to String formatting

    Quote Originally Posted by jmcilhinney
    You've got your delimiters the wrong way around so they're actually escaping the following character.

    Oh dear, I should have known it would be something obvious like that! Trust me not to see it! Thanks jmcilhinney! Once again, you've saved my bacon!

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