|
-
Sep 29th, 2009, 05:46 AM
#1
Thread Starter
New Member
[RESOLVED] convert text to dd/mm/yyyy format
I'm trying to convert the "29/09/2009" string to date format dd/mm/yyyy
I'm using visual studio 2008, and i've tried the following:
dim sdate as string
sdate = "29/09/2009"
mydate = DateTime.Parse(sDate, Globalization.CultureInfo.CreateSpecificCulture("el-GR"))
and
mydate = Date.Parse(sDate).ToShortDateString()
and i always get #9/29/2009# instead of #29/09/2009# that i want
Any help please?
Last edited by tkoletsis; Sep 29th, 2009 at 06:13 AM.
-
Sep 29th, 2009, 06:32 AM
#2
Re: convert text to dd/mm/yyyy format
Hey,
You can create a string representation of a DateTime object using String.Format:
http://msdn.microsoft.com/en-us/library/fht0f5be.aspx
Scroll to the bottom of that page, there are lots of examples!
Hope that helps!!
Gary
-
Sep 29th, 2009, 07:58 AM
#3
Re: convert text to dd/mm/yyyy format
You can't just do this?
Code:
Dim myDate As New Date("29/09/2009")
myDate.ToString("dd/MM/yyyy")
-
Sep 29th, 2009, 08:06 AM
#4
Re: convert text to dd/mm/yyyy format
 Originally Posted by tkoletsis
I'm trying to convert the "29/09/2009" string to date format dd/mm/yyyy
I'm using visual studio 2008, and i've tried the following:
dim sdate as string
sdate = "29/09/2009"
mydate = DateTime.Parse(sDate, Globalization.CultureInfo.CreateSpecificCulture("el-GR"))
and
mydate = Date.Parse(sDate).ToShortDateString()
and i always get #9/29/2009# instead of #29/09/2009# that i want
Any help please?
A Date is a Date is a Date. Dates don't have formats because they are binary values. Only string representations of dates have format. When you see #9/29/2009# that does NOT mean that your Date has that specific format. That's simply how VS represents Date literals. The value of the date is the value of the date.
Consider this. If you were to write this code:
vb.net Code:
Dim number As Integer = &HFF
and the you check the value of the variable, VS would tell you that the value was 255. Does that mean that the number has a decimal format instead of a hexadecimal format? Of course not. The number is stored in the computer in binary form, as is everything. 255 is just the way that VS represents the numeric literal.
Dates are the same. A Date is just a number stored in the computer in binary form. The number represents a number of milliseconds since a specific point in time. How can that have any format? A Date is simply a value that represents a point in time. Format is ONLY an issue when you want to display that value, at which point you need to convert it to a string, at which point you need to decide what format to use.
-
Sep 30th, 2009, 08:10 AM
#5
Thread Starter
New Member
Re: convert text to dd/mm/yyyy format
Ok i agree, but i still have problem with the following code:
Dim rows As DataRow() = dt.Select("[DateIn] = #" & tmpDate & "#")
I get that exception there:
System.FormatException was unhandled
Message="String was not recognized as a valid DateTime."
Source="mscorlib"
StackTrace:
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.Data.ConstNode..ctor(DataTable table, ValueType type, Object constant, Boolean fParseQuotes) at System.Data.ExpressionParser.Parse() at System.Data.DataExpression..ctor(DataTable table, String expression, Type type) at System.Data.Select..ctor(DataTable table, String filterExpression, String sort, DataViewRowState recordStates) at System.Data.DataTable.Select(String filterExpression)
etc...
tmpDate has the value: #2/13/2009#
Datein has the value: #2/13/2009#
My regional settings have the format dd/mm/yyyy
What can I do?
 Originally Posted by jmcilhinney
A Date is a Date is a Date. Dates don't have formats because they are binary values. Only string representations of dates have format. When you see #9/29/2009# that does NOT mean that your Date has that specific format. That's simply how VS represents Date literals. The value of the date is the value of the date.
Consider this. If you were to write this code:
vb.net Code:
Dim number As Integer = &HFF
and the you check the value of the variable, VS would tell you that the value was 255. Does that mean that the number has a decimal format instead of a hexadecimal format? Of course not. The number is stored in the computer in binary form, as is everything. 255 is just the way that VS represents the numeric literal.
Dates are the same. A Date is just a number stored in the computer in binary form. The number represents a number of milliseconds since a specific point in time. How can that have any format? A Date is simply a value that represents a point in time. Format is ONLY an issue when you want to display that value, at which point you need to convert it to a string, at which point you need to decide what format to use.
Last edited by tkoletsis; Sep 30th, 2009 at 08:19 AM.
-
Sep 30th, 2009, 07:12 PM
#6
Re: convert text to dd/mm/yyyy format
As I said, Date values themselves don't have a format but when you convert a Date to a String or vice versa, THAT is when format is important. In your case, when you use the DataTable.Select method you MUST specify dates in M/dd/yyyy format. As such, you can do this:
vb.net Code:
Dim rows As DataRow() = dt.Select("[DateIn] = #" & tmpDate.ToString("M/dd/yyyy") & "#")
or my preference would be:
vb.net Code:
Dim rows As DataRow() = dt.Select(String.Format("[DateIn] = #{0:M/dd/yyyy}#", tmpDate))
Hopefully this shows why you need to provide the full story when you first post. If we know exactly what you're trying to do then we can provide the exact solution. If you only provide half the story then you'll likely only get half answers. WHY you're trying to do what you're trying to do is usually just as important as WHAT you're trying to do.
-
Oct 1st, 2009, 03:07 AM
#7
Thread Starter
New Member
Re: convert text to dd/mm/yyyy format
Thank you, your help was very important
 Originally Posted by jmcilhinney
As I said, Date values themselves don't have a format but when you convert a Date to a String or vice versa, THAT is when format is important. In your case, when you use the DataTable.Select method you MUST specify dates in M/dd/yyyy format. As such, you can do this:
vb.net Code:
Dim rows As DataRow() = dt.Select("[DateIn] = #" & tmpDate.ToString("M/dd/yyyy") & "#")
or my preference would be:
vb.net Code:
Dim rows As DataRow() = dt.Select(String.Format("[DateIn] = #{0:M/dd/yyyy}#", tmpDate))
Hopefully this shows why you need to provide the full story when you first post. If we know exactly what you're trying to do then we can provide the exact solution. If you only provide half the story then you'll likely only get half answers. WHY you're trying to do what you're trying to do is usually just as important as WHAT you're trying to do.
-
Sep 2nd, 2010, 05:05 PM
#8
New Member
Re: convert text to dd/mm/yyyy format
Hi there i have the following code for a hotel management system and whenever i execute the program i get the following error
String was not recognized as a valid Date Time.
Can you help????
Code:
Private Sub txtDays_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDays.TextChanged
Dim tmpDate As Date
tmpDate = dtpDateIn.Value
If txtAdults.Tag = "" Then Exit Sub
ExecNonQuery("DELETE [DateIn] " & "FROM [Rate_Per_Period] " & "WHERE [DateIn]>#" & dtpDateOut.Value.AddDays(-1) & "#")
Dim intAdults As Short
If txtAdults.Text = "" Then Exit Sub
If CDbl(txtAdults.Text) = hsAdults.Minimum Then
intAdults = 0
Else
intAdults = CShort(txtAdults.Text) - hsAdults.Minimum
End If
Dim qry As String = "SELECT * FROM Rate_Per_Period WHERE FolioNumber = '" & txtFolioNumber.Text & "' ORDER BY DateIn"
Dim cnHotel As OleDbConnection = New OleDbConnection(cnString)
Try
Dim da As New OleDbDataAdapter()
da.SelectCommand = New OleDbCommand(qry, cnHotel)
Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(da)
Dim ds As New DataSet()
da.Fill(ds, "Rate_Per_Period")
Dim dt As DataTable = ds.Tables("Rate_Per_Period")
Dim newRow As DataRow
Do Until tmpDate > dtpDateOut.Value.AddDays(-1)
Dim rows As DataRow() = dt.Select("[DateIn] = #" & tmpDate & "#")
If rows.Count = 0 Then
newRow = dt.NewRow()
newRow("FolioNumber") = txtFolioNumber.Text
newRow("DateIn") = tmpDate
newRow("RoomNumber") = txtRoomNumber.Text
newRow("RateTypeID") = cboRateType.SelectedValue
newRow("Rate") = txtRate.Text
newRow("Adults") = CDbl(txtAdults.Tag) * intAdults
newRow("Childrens") = CDbl(toMoney(txtChildrens.Tag)) * toNumber(txtChildrens.Text)
dt.Rows.Add(newRow)
End If
tmpDate = System.DateTime.FromOADate(tmpDate.ToOADate + 1)
Loop
da.Update(ds, "Rate_Per_Period")
Catch ex As OleDbException
MsgBox(ex.ToString)
Finally
cnHotel.Close()
End Try
End Sub
-
Sep 2nd, 2010, 11:11 PM
#9
Re: convert text to dd/mm/yyyy format
 Originally Posted by dgichane
Hi there i have the following code for a hotel management system and whenever i execute the program i get the following error
String was not recognized as a valid Date Time.
Can you help????
If you're going to post to an existing thread then you should at least read it first. Post #6 addresses that specifically, so the answer is already there and you simply ignored it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|