Home All Groups Group Topic Archive Search About

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

Author
26 Jun 2006 12:25 PM
Richiep
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(assessmentDate,
"dd/MM/yyyy", Nothing)


    Private Sub getDate2()
        Dim assessmentDate As Date
        Dim MyCulture As New System.Globalization.CultureInfo("en-GB")
        Try
            assessmentDate =
System.DateTime.Parse(txtAssessmentDate.Text.Trim, MyCulture)
        Catch ex As Exception
            lblValidAssessmentDate.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.Parameters.Add("@assessmentDate", SqlDbType.DateTime, 11)
        command.Parameters("@assessmentDate").Value = assessmentDate

Thank you anyone who helps.

Sincerely,
Richard.

Author
26 Jun 2006 12:54 PM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Richiep" <Rich***@discussions.microsoft.com> schreef in bericht
news:27ADD6D7-2198-43EB-BCA1-7F057507E849@microsoft.com...
>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(assessmentDate,
> "dd/MM/yyyy", Nothing)
>
>
>    Private Sub getDate2()
>        Dim assessmentDate As Date
>        Dim MyCulture As New System.Globalization.CultureInfo("en-GB")
>        Try
>            assessmentDate =
> System.DateTime.Parse(txtAssessmentDate.Text.Trim, MyCulture)
>        Catch ex As Exception
>            lblValidAssessmentDate.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.Parameters.Add("@assessmentDate", SqlDbType.DateTime, 11)
>        command.Parameters("@assessmentDate").Value = assessmentDate
>
> Thank you anyone who helps.
>
> Sincerely,
> Richard.
>
>
Author
26 Jun 2006 1:20 PM
Richiep
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.

Show quoteHide quote
"Cor Ligthert [MVP]" wrote:

> 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" <Rich***@discussions.microsoft.com> schreef in bericht
> news:27ADD6D7-2198-43EB-BCA1-7F057507E849@microsoft.com...
> >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(assessmentDate,
> > "dd/MM/yyyy", Nothing)
> >
> >
> >    Private Sub getDate2()
> >        Dim assessmentDate As Date
> >        Dim MyCulture As New System.Globalization.CultureInfo("en-GB")
> >        Try
> >            assessmentDate =
> > System.DateTime.Parse(txtAssessmentDate.Text.Trim, MyCulture)
> >        Catch ex As Exception
> >            lblValidAssessmentDate.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.Parameters.Add("@assessmentDate", SqlDbType.DateTime, 11)
> >        command.Parameters("@assessmentDate").Value = assessmentDate
> >
> > Thank you anyone who helps.
> >
> > Sincerely,
> > Richard.
> >
> >
>
>
>
Author
26 Jun 2006 1:34 PM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Richiep" <Rich***@discussions.microsoft.com> schreef in bericht
news:18318D9A-1145-4B3E-9868-730C8B121D40@microsoft.com...
> 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:
>
>> 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" <Rich***@discussions.microsoft.com> schreef in bericht
>> news:27ADD6D7-2198-43EB-BCA1-7F057507E849@microsoft.com...
>> >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(assessmentDate,
>> > "dd/MM/yyyy", Nothing)
>> >
>> >
>> >    Private Sub getDate2()
>> >        Dim assessmentDate As Date
>> >        Dim MyCulture As New System.Globalization.CultureInfo("en-GB")
>> >        Try
>> >            assessmentDate =
>> > System.DateTime.Parse(txtAssessmentDate.Text.Trim, MyCulture)
>> >        Catch ex As Exception
>> >            lblValidAssessmentDate.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.Parameters.Add("@assessmentDate", SqlDbType.DateTime,
>> > 11)
>> >        command.Parameters("@assessmentDate").Value = assessmentDate
>> >
>> > Thank you anyone who helps.
>> >
>> > Sincerely,
>> > Richard.
>> >
>> >
>>
>>
>>
Author
26 Jun 2006 1:48 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Richiep" <Rich***@discussions.microsoft.com> schrieb:
>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(assessmentDate,
> "dd/MM/yyyy", Nothing)

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/>
Author
26 Jun 2006 1:48 PM
Larry Lard
Richiep wrote:
Show quoteHide quote
> 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?

>     Private Sub getDate2()
>         Dim assessmentDate As Date
>         Dim MyCulture As New System.Globalization.CultureInfo("en-GB")
>         Try
>             assessmentDate =
> System.DateTime.Parse(txtAssessmentDate.Text.Trim, MyCulture)
>         Catch ex As Exception
>             lblValidAssessmentDate.Text = "Assessment date: not a valid date
> in UK format"
>         End Try
>     End Sub


When I try it, it does.

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

Show us the code that displays the date.

>
> 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(assessmentDate,
> "dd/MM/yyyy", Nothing)

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
Author
26 Jun 2006 2:48 PM
Richiep
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 :
    <globalization requestEncoding="utf-8" responseEncoding="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">Assessment
Date:</asp:label>
<asp:textbox id="txtAssessmentDate" runat="server" Width="88px" Wrap="False"
ReadOnly="True"></asp:textbox>
<A
onclick="window.open('../calendar/popup.aspx?textbox=txtAssessmentDate','cal','width=250,height=225,left=270,top=180')"
href="javascript:;"> <IMG src="../images/SmallCalendar.gif" border="0"></A>
</p>

From the calendar to the textbox:

Protected Sub Change_Date(ByVal sender As System.Object, ByVal e As
System.EventArgs)
    Dim strScript As String = "<script>window.opener.document.forms(0)." +
control.Value + ".value = '"
    strScript += calDate.SelectedDate.ToString("dd/MM/yyyy")
    strScript += "';self.close()"
    strScript += "</" + "script>"
    RegisterClientScriptBlock("anything", 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 = txtAssessmentDate.Text.Trim
'       Dim dt As dateTime = System.DateTime.ParseExact(assessmentDate,
"dd/mm/yyyy", Nothing)

    'pass module details into the routine to insert it into SQL server.
    moduleInsertStatus = clsModules.spModules_Ins(moduleCode, moduleName,
level, courseworks, noOfCourseworks, leader, location, startTimeHours,
startTimeMinutes, endTimeHours, endTimeMinutes, assessmentDate)

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

ByVal assessmentDate As System.DateTime

command.Parameters.Add("@assessmentDate", SqlDbType.DateTime, 11)
command.Parameters("@assessmentDate").Value = assessmentDate



Show quoteHide quote
"Larry Lard" wrote:

>
> Richiep wrote:
> > 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?
>
> >     Private Sub getDate2()
> >         Dim assessmentDate As Date
> >         Dim MyCulture As New System.Globalization.CultureInfo("en-GB")
> >         Try
> >             assessmentDate =
> > System.DateTime.Parse(txtAssessmentDate.Text.Trim, MyCulture)
> >         Catch ex As Exception
> >             lblValidAssessmentDate.Text = "Assessment date: not a valid date
> > in UK format"
> >         End Try
> >     End Sub
>
>
> When I try it, it does.
>
> >
> > The date returned is #12:00:00 AM# but the date I entered into the text box
> > was 24/06/2006.
>
> Show us the code that displays the date.
>
> >
> > 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(assessmentDate,
> > "dd/MM/yyyy", Nothing)
>
> 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
>
>
Author
26 Jun 2006 3:25 PM
Cor Ligthert [MVP]
Hi,

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

Cor

Show quoteHide quote
"Richiep" <Rich***@discussions.microsoft.com> schreef in bericht
news:9FD92278-5714-4358-A97B-773BA1C0A57B@microsoft.com...
> 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 :
>    <globalization requestEncoding="utf-8" responseEncoding="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">Assessment
> Date:</asp:label>
> <asp:textbox id="txtAssessmentDate" runat="server" Width="88px"
> Wrap="False"
> ReadOnly="True"></asp:textbox>
> <A
> onclick="window.open('../calendar/popup.aspx?textbox=txtAssessmentDate','cal','width=250,height=225,left=270,top=180')"
> href="javascript:;"> <IMG src="../images/SmallCalendar.gif"
> border="0"></A>
> </p>
>
> From the calendar to the textbox:
>
> Protected Sub Change_Date(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
>    Dim strScript As String = "<script>window.opener.document.forms(0)." +
> control.Value + ".value = '"
>    strScript += calDate.SelectedDate.ToString("dd/MM/yyyy")
>    strScript += "';self.close()"
>    strScript += "</" + "script>"
>    RegisterClientScriptBlock("anything", 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 = txtAssessmentDate.Text.Trim
> '       Dim dt As dateTime = System.DateTime.ParseExact(assessmentDate,
> "dd/mm/yyyy", Nothing)
>
> 'pass module details into the routine to insert it into SQL server.
> moduleInsertStatus = clsModules.spModules_Ins(moduleCode, moduleName,
> level, courseworks, noOfCourseworks, leader, location, startTimeHours,
> startTimeMinutes, endTimeHours, endTimeMinutes, assessmentDate)
>
> spModules_Ins has code to call the stored procedure sql parameters are:
>
> ByVal assessmentDate As System.DateTime
>
> command.Parameters.Add("@assessmentDate", SqlDbType.DateTime, 11)
> command.Parameters("@assessmentDate").Value = assessmentDate
>
>
>
> "Larry Lard" wrote:
>
>>
>> Richiep wrote:
>> > 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?
>>
>> >     Private Sub getDate2()
>> >         Dim assessmentDate As Date
>> >         Dim MyCulture As New System.Globalization.CultureInfo("en-GB")
>> >         Try
>> >             assessmentDate =
>> > System.DateTime.Parse(txtAssessmentDate.Text.Trim, MyCulture)
>> >         Catch ex As Exception
>> >             lblValidAssessmentDate.Text = "Assessment date: not a valid
>> > date
>> > in UK format"
>> >         End Try
>> >     End Sub
>>
>>
>> When I try it, it does.
>>
>> >
>> > The date returned is #12:00:00 AM# but the date I entered into the text
>> > box
>> > was 24/06/2006.
>>
>> Show us the code that displays the date.
>>
>> >
>> > 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(assessmentDate,
>> > "dd/MM/yyyy", Nothing)
>>
>> 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
>>
>>