473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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(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.
Jun 26 '06 #1
7 6786
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" <Ri*****@discussions.microsoft.com> schreef in bericht
news:27**********************************@microsof t.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.

Jun 26 '06 #2
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" <Ri*****@discussions.microsoft.com> schreef in bericht
news:27**********************************@microsof t.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.


Jun 26 '06 #3
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" <Ri*****@discussions.microsoft.com> schreef in bericht
news:18**********************************@microsof t.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" <Ri*****@discussions.microsoft.com> schreef in bericht
news:27**********************************@microsof t.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.
>
>


Jun 26 '06 #4

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

Jun 26 '06 #5
"Richiep" <Ri*****@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/>

Jun 26 '06 #6
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

Jun 26 '06 #7
Hi,

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

Cor

"Richiep" <Ri*****@discussions.microsoft.com> schreef in bericht
news:9F**********************************@microsof t.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

Jun 26 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: OKI | last post by:
Hi. I had a VB program that was using an SQL Server 6.5 DB. Date format was dd/mm/yyyy. Now the DB was changed to an SQL Server 7 and the date format is yyyy/mm/dd. How can I do for changing...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
5
by: goochey | last post by:
I'm trying to convert a Julian Date (Format "4365") into an actual calendar date in Visual Basic, can anyone help me out with this.
1
by: Peter Nolan | last post by:
Hi All, I promise I did my homework and looked around, but I can't find a simple date/datetime converter in C or C++. I think there might be one in YACL and things like that but it's pretty huge...
12
by: DC Gringo | last post by:
How can I convert this pubLatest to a date with format "m/d/yyyy"? Dim pubLatest As New Date pubLatest = Me.SqlSelectCommand1.Parameters("@pubLatest").Value -- _____ DC G
9
by: David Rysdam | last post by:
I have a large amount of data that I copy in and out of Sybase very often. Now I also want to copy this data in and out of postgres. I have an existing script that creates the entire database(s)...
9
by: Bob Sanderson | last post by:
I have a field in a database called DateRcvd. At present, it outputs in my report in the yyyy-mm-dd format. I would like it to display in the dd/mm/yy format. What is the easiest way to accomplish...
10
by: DontellTrevell via AccessMonster.com | last post by:
HELP!!....I need to calculate the numer of days elapsed between two field. But, the date format is YYYYMMDD. How can i accomplsh this? -- Dontell Trevell Message posted via AccessMonster.com...
3
by: dave | last post by:
I need to compute an expiration date based on the number of hours, days, or months purchased. The expiration date needs to be expressed in minutes something like '1260481600'. How can I get the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.