ASP.NET web application - date conversions UK date format?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richiep

    #1

    ASP.NET web application - date conversions UK date format?

    I am trying to get a UK format date of dd/mm/yyyy.

    Why does the following subroutine not return a valid date in a web form?

    The date returned is #12:00:00 AM# but the date I entered into the text box
    was 24/06/2006.

    The other solution I have tried is given by the following two lines that do
    not compile because a value of date type cannot be converted to datetime:

    Dim assessmentDate As String = "24/06/2006"
    Dim dt As dateTime = System.DateTime .ParseExact(ass essmentDate,
    "dd/MM/yyyy", Nothing)


    Private Sub getDate2()
    Dim assessmentDate As Date
    Dim MyCulture As New System.Globaliz ation.CultureIn fo("en-GB")
    Try
    assessmentDate =
    System.DateTime .Parse(txtAsses smentDate.Text. Trim, MyCulture)
    Catch ex As Exception
    lblValidAssessm entDate.Text = "Assessment date: not a valid date
    in UK format"
    End Try
    End Sub


    The date is retrieved from my calendar control into the text box ok as
    dd/mm/yyyy. Then I want to take it from the textbox and convert it into a
    datetime so that I can store it in a SQL server datetime column. Here is the
    parameter being passed into the VB function that calls the store procedure:

    ByVal assessmentDate As System.DateTime ,

    Here is the SQL parameter:

    command.Paramet ers.Add("@asses smentDate", SqlDbType.DateT ime, 11)
    command.Paramet ers("@assessmen tDate").Value = assessmentDate

    Thank you anyone who helps.

    Sincerely,
    Richard.


  • Cor Ligthert [MVP]

    #2
    Re: ASP.NET web application - date conversions UK date format?

    Richiep,


    As far as I can see is all your code in my opinion correct, although you are
    writting that you retrieve the date as dd/mm/yyyy; If that is true that
    means day minutes year.

    If your computer is set to UK and not to US, than this is enough to make
    from a British date a datetime value.

    dim dtm as datatime = CDate("24/06/2006)
    To get it back is than
    dim str as string = dtm.ToString("d ") ' although the month is than given in
    a single 6

    However be aware that with a Webpage, you cannot be sure what the user has
    typed in, if you did not have displayed the patern on that page. As well do
    not all server (at least in Holland) have the right country settings and are
    often Installed with US settings.

    Cor

    "Richiep" <Richiep@discus sions.microsoft .com> schreef in bericht
    news:27ADD6D7-2198-43EB-BCA1-7F057507E849@mi crosoft.com...[color=blue]
    >I am trying to get a UK format date of dd/mm/yyyy.
    >
    > Why does the following subroutine not return a valid date in a web form?
    >
    > The date returned is #12:00:00 AM# but the date I entered into the text
    > box
    > was 24/06/2006.
    >
    > The other solution I have tried is given by the following two lines that
    > do
    > not compile because a value of date type cannot be converted to datetime:
    >
    > Dim assessmentDate As String = "24/06/2006"
    > Dim dt As dateTime = System.DateTime .ParseExact(ass essmentDate,
    > "dd/MM/yyyy", Nothing)
    >
    >
    > Private Sub getDate2()
    > Dim assessmentDate As Date
    > Dim MyCulture As New System.Globaliz ation.CultureIn fo("en-GB")
    > Try
    > assessmentDate =
    > System.DateTime .Parse(txtAsses smentDate.Text. Trim, MyCulture)
    > Catch ex As Exception
    > lblValidAssessm entDate.Text = "Assessment date: not a valid
    > date
    > in UK format"
    > End Try
    > End Sub
    >
    >
    > The date is retrieved from my calendar control into the text box ok as
    > dd/mm/yyyy. Then I want to take it from the textbox and convert it into a
    > datetime so that I can store it in a SQL server datetime column. Here is
    > the
    > parameter being passed into the VB function that calls the store
    > procedure:
    >
    > ByVal assessmentDate As System.DateTime ,
    >
    > Here is the SQL parameter:
    >
    > command.Paramet ers.Add("@asses smentDate", SqlDbType.DateT ime, 11)
    > command.Paramet ers("@assessmen tDate").Value = assessmentDate
    >
    > Thank you anyone who helps.
    >
    > Sincerely,
    > Richard.
    >
    >[/color]


    Comment

    • Richiep

      #3
      Re: ASP.NET web application - date conversions UK date format?

      Thanks Cor,

      But I get a runtime exception:
      Cast from string "24/06/2006" to type 'Date' is not valid.

      On line:
      Dim dtm As System.DateTime = CDate("24/06/2006")

      The code entered in the code behind to the form is :

      Dim dtm As System.DateTime = CDate("24/06/2006")
      Dim assessmentDate As String = dtm.ToString("d ") ' although the month is
      than given in a single 6

      The pattern entered by the user is always dd/mm/yyyy days month year because
      a calendar control is used to format the dates and the textbox is readonly.
      The calendar is the only way they can enter the date. But the conversion
      still is a problem.

      Thanks again.
      Richard.

      "Cor Ligthert [MVP]" wrote:
      [color=blue]
      > Richiep,
      >
      >
      > As far as I can see is all your code in my opinion correct, although you are
      > writting that you retrieve the date as dd/mm/yyyy; If that is true that
      > means day minutes year.
      >
      > If your computer is set to UK and not to US, than this is enough to make
      > from a British date a datetime value.
      >
      > dim dtm as datatime = CDate("24/06/2006)
      > To get it back is than
      > dim str as string = dtm.ToString("d ") ' although the month is than given in
      > a single 6
      >
      > However be aware that with a Webpage, you cannot be sure what the user has
      > typed in, if you did not have displayed the patern on that page. As well do
      > not all server (at least in Holland) have the right country settings and are
      > often Installed with US settings.
      >
      > Cor
      >
      > "Richiep" <Richiep@discus sions.microsoft .com> schreef in bericht
      > news:27ADD6D7-2198-43EB-BCA1-7F057507E849@mi crosoft.com...[color=green]
      > >I am trying to get a UK format date of dd/mm/yyyy.
      > >
      > > Why does the following subroutine not return a valid date in a web form?
      > >
      > > The date returned is #12:00:00 AM# but the date I entered into the text
      > > box
      > > was 24/06/2006.
      > >
      > > The other solution I have tried is given by the following two lines that
      > > do
      > > not compile because a value of date type cannot be converted to datetime:
      > >
      > > Dim assessmentDate As String = "24/06/2006"
      > > Dim dt As dateTime = System.DateTime .ParseExact(ass essmentDate,
      > > "dd/MM/yyyy", Nothing)
      > >
      > >
      > > Private Sub getDate2()
      > > Dim assessmentDate As Date
      > > Dim MyCulture As New System.Globaliz ation.CultureIn fo("en-GB")
      > > Try
      > > assessmentDate =
      > > System.DateTime .Parse(txtAsses smentDate.Text. Trim, MyCulture)
      > > Catch ex As Exception
      > > lblValidAssessm entDate.Text = "Assessment date: not a valid
      > > date
      > > in UK format"
      > > End Try
      > > End Sub
      > >
      > >
      > > The date is retrieved from my calendar control into the text box ok as
      > > dd/mm/yyyy. Then I want to take it from the textbox and convert it into a
      > > datetime so that I can store it in a SQL server datetime column. Here is
      > > the
      > > parameter being passed into the VB function that calls the store
      > > procedure:
      > >
      > > ByVal assessmentDate As System.DateTime ,
      > >
      > > Here is the SQL parameter:
      > >
      > > command.Paramet ers.Add("@asses smentDate", SqlDbType.DateT ime, 11)
      > > command.Paramet ers("@assessmen tDate").Value = assessmentDate
      > >
      > > Thank you anyone who helps.
      > >
      > > Sincerely,
      > > Richard.
      > >
      > >[/color]
      >
      >
      >[/color]

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: ASP.NET web application - date conversions UK date format?

        Richiep,

        Because of the fact that we in Holland use normally 24-06-2006, have I
        tested it with that format and the format "24/06/2006". Are you sure that
        the country setting of your computer is UK and not US or that somebody has
        modified those settings?

        As far as the AM/PM indicator has Brittain no other format than Holland in
        my idea?

        Cor

        "Richiep" <Richiep@discus sions.microsoft .com> schreef in bericht
        news:18318D9A-1145-4B3E-9868-730C8B121D40@mi crosoft.com...[color=blue]
        > Thanks Cor,
        >
        > But I get a runtime exception:
        > Cast from string "24/06/2006" to type 'Date' is not valid.
        >
        > On line:
        > Dim dtm As System.DateTime = CDate("24/06/2006")
        >
        > The code entered in the code behind to the form is :
        >
        > Dim dtm As System.DateTime = CDate("24/06/2006")
        > Dim assessmentDate As String = dtm.ToString("d ") ' although the month is
        > than given in a single 6
        >
        > The pattern entered by the user is always dd/mm/yyyy days month year
        > because
        > a calendar control is used to format the dates and the textbox is
        > readonly.
        > The calendar is the only way they can enter the date. But the conversion
        > still is a problem.
        >
        > Thanks again.
        > Richard.
        >
        > "Cor Ligthert [MVP]" wrote:
        >[color=green]
        >> Richiep,
        >>
        >>
        >> As far as I can see is all your code in my opinion correct, although you
        >> are
        >> writting that you retrieve the date as dd/mm/yyyy; If that is true that
        >> means day minutes year.
        >>
        >> If your computer is set to UK and not to US, than this is enough to make
        >> from a British date a datetime value.
        >>
        >> dim dtm as datatime = CDate("24/06/2006)
        >> To get it back is than
        >> dim str as string = dtm.ToString("d ") ' although the month is than given
        >> in
        >> a single 6
        >>
        >> However be aware that with a Webpage, you cannot be sure what the user
        >> has
        >> typed in, if you did not have displayed the patern on that page. As well
        >> do
        >> not all server (at least in Holland) have the right country settings and
        >> are
        >> often Installed with US settings.
        >>
        >> Cor
        >>
        >> "Richiep" <Richiep@discus sions.microsoft .com> schreef in bericht
        >> news:27ADD6D7-2198-43EB-BCA1-7F057507E849@mi crosoft.com...[color=darkred]
        >> >I am trying to get a UK format date of dd/mm/yyyy.
        >> >
        >> > Why does the following subroutine not return a valid date in a web
        >> > form?
        >> >
        >> > The date returned is #12:00:00 AM# but the date I entered into the text
        >> > box
        >> > was 24/06/2006.
        >> >
        >> > The other solution I have tried is given by the following two lines
        >> > that
        >> > do
        >> > not compile because a value of date type cannot be converted to
        >> > datetime:
        >> >
        >> > Dim assessmentDate As String = "24/06/2006"
        >> > Dim dt As dateTime = System.DateTime .ParseExact(ass essmentDate,
        >> > "dd/MM/yyyy", Nothing)
        >> >
        >> >
        >> > Private Sub getDate2()
        >> > Dim assessmentDate As Date
        >> > Dim MyCulture As New System.Globaliz ation.CultureIn fo("en-GB")
        >> > Try
        >> > assessmentDate =
        >> > System.DateTime .Parse(txtAsses smentDate.Text. Trim, MyCulture)
        >> > Catch ex As Exception
        >> > lblValidAssessm entDate.Text = "Assessment date: not a valid
        >> > date
        >> > in UK format"
        >> > End Try
        >> > End Sub
        >> >
        >> >
        >> > The date is retrieved from my calendar control into the text box ok as
        >> > dd/mm/yyyy. Then I want to take it from the textbox and convert it into
        >> > a
        >> > datetime so that I can store it in a SQL server datetime column. Here
        >> > is
        >> > the
        >> > parameter being passed into the VB function that calls the store
        >> > procedure:
        >> >
        >> > ByVal assessmentDate As System.DateTime ,
        >> >
        >> > Here is the SQL parameter:
        >> >
        >> > command.Paramet ers.Add("@asses smentDate", SqlDbType.DateT ime,
        >> > 11)
        >> > command.Paramet ers("@assessmen tDate").Value = assessmentDate
        >> >
        >> > Thank you anyone who helps.
        >> >
        >> > Sincerely,
        >> > Richard.
        >> >
        >> >[/color]
        >>
        >>
        >>[/color][/color]


        Comment

        • Larry Lard

          #5
          Re: ASP.NET web application - date conversions UK date format?


          Richiep wrote:[color=blue]
          > I am trying to get a UK format date of dd/mm/yyyy.
          >
          > Why does the following subroutine not return a valid date in a web form?[/color]
          [color=blue]
          > Private Sub getDate2()
          > Dim assessmentDate As Date
          > Dim MyCulture As New System.Globaliz ation.CultureIn fo("en-GB")
          > Try
          > assessmentDate =
          > System.DateTime .Parse(txtAsses smentDate.Text. Trim, MyCulture)
          > Catch ex As Exception
          > lblValidAssessm entDate.Text = "Assessment date: not a valid date
          > in UK format"
          > End Try
          > End Sub[/color]


          When I try it, it does.
          [color=blue]
          >
          > The date returned is #12:00:00 AM# but the date I entered into the text box
          > was 24/06/2006.[/color]

          Show us the code that displays the date.
          [color=blue]
          >
          > The other solution I have tried is given by the following two lines that do
          > not compile because a value of date type cannot be converted to datetime:
          >
          > Dim assessmentDate As String = "24/06/2006"
          > Dim dt As dateTime = System.DateTime .ParseExact(ass essmentDate,
          > "dd/MM/yyyy", Nothing)[/color]

          That compiles just fine when I paste it into VS2003 and VS2005... I'm
          not sure how you're getting those to error.

          --
          Larry Lard
          Replies to group please

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: ASP.NET web application - date conversions UK date format?

            "Richiep" <Richiep@discus sions.microsoft .com> schrieb:[color=blue]
            >I am trying to get a UK format date of dd/mm/yyyy.
            >
            > Why does the following subroutine not return a valid date in a web form?
            >
            > The date returned is #12:00:00 AM# but the date I entered into the text
            > box
            > was 24/06/2006.
            >
            > The other solution I have tried is given by the following two lines that
            > do
            > not compile because a value of date type cannot be converted to datetime:
            >
            > Dim assessmentDate As String = "24/06/2006"
            > Dim dt As dateTime = System.DateTime .ParseExact(ass essmentDate,
            > "dd/MM/yyyy", Nothing)[/color]

            Use "dd\/MM\/yyyy" as date format pattern that is passed to 'ParseExact'.

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://classicvb.org/petition/>

            Comment

            • Richiep

              #7
              Re: ASP.NET web application - date conversions UK date format?

              Thank you very much to everyone who has helped. See my response to your posts
              below.

              I checked regional settings via the control panel regional and language
              option. It is all in UK.

              The web.config has a globalizatoin option of :
              <globalizatio n requestEncoding ="utf-8" responseEncodin g="utf-8"
              uiCulture="en-GB" />

              I tried using dd\/MM\/yyyy" as date format pattern that is passed to
              'ParseExact'.
              but it does not compile because a value of date type cannot be converted to
              datetime.

              I am using VS 2003. I post the code below.

              When you say the code that displayys the date do you mean the calendar code
              that loads it into the textbox where it is displayed (read only).

              If so, here it is:

              <p>
              <asp:label id="lblDay" runat="server" Height="18px" Width="184px">A ssessment
              Date:</asp:label>
              <asp:textbox id="txtAssessme ntDate" runat="server" Width="88px" Wrap="False"
              ReadOnly="True" ></asp:textbox>
              <A
              onclick="window .open('../calendar/popup.aspx?text box=txtAssessme ntDate','cal',' width=250,heigh t=225,left=270, top=180')"
              href="javascrip t:;"> <IMG src="../images/SmallCalendar.g if" border="0"></A>
              </p>

              From the calendar to the textbox:

              Protected Sub Change_Date(ByV al sender As System.Object, ByVal e As
              System.EventArg s)
              Dim strScript As String = "<script>window .opener.documen t.forms(0)." +
              control.Value + ".value = '"
              strScript += calDate.Selecte dDate.ToString( "dd/MM/yyyy")
              strScript += "';self.close() "
              strScript += "</" + "script>"
              RegisterClientS criptBlock("any thing", strScript)
              End Sub

              The date is visible in the textbox whhen I press the next button. This
              results in a sub that runs the code below (commented out because I cannot get
              it to work).


              ' Dim dtm As System.DateTime = CDate("24/06/2006")
              ' Dim assessmentDate As String = dtm.ToString("d ") ' although the month is
              than given in a single 6
              ' Dim assessmentDate As String
              ' assessmentDate = txtAssessmentDa te.Text.Trim
              ' Dim dt As dateTime = System.DateTime .ParseExact(ass essmentDate,
              "dd/mm/yyyy", Nothing)

              'pass module details into the routine to insert it into SQL server.
              moduleInsertSta tus = clsModules.spMo dules_Ins(modul eCode, moduleName,
              level, courseworks, noOfCourseworks , leader, location, startTimeHours,
              startTimeMinute s, endTimeHours, endTimeMinutes, assessmentDate)

              spModules_Ins has code to call the stored procedure sql parameters are:

              ByVal assessmentDate As System.DateTime

              command.Paramet ers.Add("@asses smentDate", SqlDbType.DateT ime, 11)
              command.Paramet ers("@assessmen tDate").Value = assessmentDate



              "Larry Lard" wrote:
              [color=blue]
              >
              > Richiep wrote:[color=green]
              > > I am trying to get a UK format date of dd/mm/yyyy.
              > >
              > > Why does the following subroutine not return a valid date in a web form?[/color]
              >[color=green]
              > > Private Sub getDate2()
              > > Dim assessmentDate As Date
              > > Dim MyCulture As New System.Globaliz ation.CultureIn fo("en-GB")
              > > Try
              > > assessmentDate =
              > > System.DateTime .Parse(txtAsses smentDate.Text. Trim, MyCulture)
              > > Catch ex As Exception
              > > lblValidAssessm entDate.Text = "Assessment date: not a valid date
              > > in UK format"
              > > End Try
              > > End Sub[/color]
              >
              >
              > When I try it, it does.
              >[color=green]
              > >
              > > The date returned is #12:00:00 AM# but the date I entered into the text box
              > > was 24/06/2006.[/color]
              >
              > Show us the code that displays the date.
              >[color=green]
              > >
              > > The other solution I have tried is given by the following two lines that do
              > > not compile because a value of date type cannot be converted to datetime:
              > >
              > > Dim assessmentDate As String = "24/06/2006"
              > > Dim dt As dateTime = System.DateTime .ParseExact(ass essmentDate,
              > > "dd/MM/yyyy", Nothing)[/color]
              >
              > That compiles just fine when I paste it into VS2003 and VS2005... I'm
              > not sure how you're getting those to error.
              >
              > --
              > Larry Lard
              > Replies to group please
              >
              >[/color]

              Comment

              • Cor Ligthert [MVP]

                #8
                Re: ASP.NET web application - date conversions UK date format?

                Hi,

                Are you sure that it is en-GB? I use (I thought) always en-UK when I need
                that setting.

                Cor

                "Richiep" <Richiep@discus sions.microsoft .com> schreef in bericht
                news:9FD92278-5714-4358-A97B-773BA1C0A57B@mi crosoft.com...[color=blue]
                > Thank you very much to everyone who has helped. See my response to your
                > posts
                > below.
                >
                > I checked regional settings via the control panel regional and language
                > option. It is all in UK.
                >
                > The web.config has a globalizatoin option of :
                > <globalizatio n requestEncoding ="utf-8" responseEncodin g="utf-8"
                > uiCulture="en-GB" />
                >
                > I tried using dd\/MM\/yyyy" as date format pattern that is passed to
                > 'ParseExact'.
                > but it does not compile because a value of date type cannot be converted
                > to
                > datetime.
                >
                > I am using VS 2003. I post the code below.
                >
                > When you say the code that displayys the date do you mean the calendar
                > code
                > that loads it into the textbox where it is displayed (read only).
                >
                > If so, here it is:
                >
                > <p>
                > <asp:label id="lblDay" runat="server" Height="18px"
                > Width="184px">A ssessment
                > Date:</asp:label>
                > <asp:textbox id="txtAssessme ntDate" runat="server" Width="88px"
                > Wrap="False"
                > ReadOnly="True" ></asp:textbox>
                > <A
                > onclick="window .open('../calendar/popup.aspx?text box=txtAssessme ntDate','cal',' width=250,heigh t=225,left=270, top=180')"
                > href="javascrip t:;"> <IMG src="../images/SmallCalendar.g if"
                > border="0"></A>
                > </p>
                >
                > From the calendar to the textbox:
                >
                > Protected Sub Change_Date(ByV al sender As System.Object, ByVal e As
                > System.EventArg s)
                > Dim strScript As String = "<script>window .opener.documen t.forms(0)." +
                > control.Value + ".value = '"
                > strScript += calDate.Selecte dDate.ToString( "dd/MM/yyyy")
                > strScript += "';self.close() "
                > strScript += "</" + "script>"
                > RegisterClientS criptBlock("any thing", strScript)
                > End Sub
                >
                > The date is visible in the textbox whhen I press the next button. This
                > results in a sub that runs the code below (commented out because I cannot
                > get
                > it to work).
                >
                >
                > ' Dim dtm As System.DateTime = CDate("24/06/2006")
                > ' Dim assessmentDate As String = dtm.ToString("d ") ' although the month
                > is
                > than given in a single 6
                > ' Dim assessmentDate As String
                > ' assessmentDate = txtAssessmentDa te.Text.Trim
                > ' Dim dt As dateTime = System.DateTime .ParseExact(ass essmentDate,
                > "dd/mm/yyyy", Nothing)
                >
                > 'pass module details into the routine to insert it into SQL server.
                > moduleInsertSta tus = clsModules.spMo dules_Ins(modul eCode, moduleName,
                > level, courseworks, noOfCourseworks , leader, location, startTimeHours,
                > startTimeMinute s, endTimeHours, endTimeMinutes, assessmentDate)
                >
                > spModules_Ins has code to call the stored procedure sql parameters are:
                >
                > ByVal assessmentDate As System.DateTime
                >
                > command.Paramet ers.Add("@asses smentDate", SqlDbType.DateT ime, 11)
                > command.Paramet ers("@assessmen tDate").Value = assessmentDate
                >
                >
                >
                > "Larry Lard" wrote:
                >[color=green]
                >>
                >> Richiep wrote:[color=darkred]
                >> > I am trying to get a UK format date of dd/mm/yyyy.
                >> >
                >> > Why does the following subroutine not return a valid date in a web
                >> > form?[/color]
                >>[color=darkred]
                >> > Private Sub getDate2()
                >> > Dim assessmentDate As Date
                >> > Dim MyCulture As New System.Globaliz ation.CultureIn fo("en-GB")
                >> > Try
                >> > assessmentDate =
                >> > System.DateTime .Parse(txtAsses smentDate.Text. Trim, MyCulture)
                >> > Catch ex As Exception
                >> > lblValidAssessm entDate.Text = "Assessment date: not a valid
                >> > date
                >> > in UK format"
                >> > End Try
                >> > End Sub[/color]
                >>
                >>
                >> When I try it, it does.
                >>[color=darkred]
                >> >
                >> > The date returned is #12:00:00 AM# but the date I entered into the text
                >> > box
                >> > was 24/06/2006.[/color]
                >>
                >> Show us the code that displays the date.
                >>[color=darkred]
                >> >
                >> > The other solution I have tried is given by the following two lines
                >> > that do
                >> > not compile because a value of date type cannot be converted to
                >> > datetime:
                >> >
                >> > Dim assessmentDate As String = "24/06/2006"
                >> > Dim dt As dateTime = System.DateTime .ParseExact(ass essmentDate,
                >> > "dd/MM/yyyy", Nothing)[/color]
                >>
                >> That compiles just fine when I paste it into VS2003 and VS2005... I'm
                >> not sure how you're getting those to error.
                >>
                >> --
                >> Larry Lard
                >> Replies to group please
                >>
                >>[/color][/color]


                Comment

                Working...