I have a datatable (returned by database using dataadaptor) with a DateOfBirth field with 00:00:00 at the end of date of birth. I want to remove those 00:00:00 so I wrote a function but it doesn't do the job and I don't know why. Can anyone suggest to me how to remove the 00:00:00 from DateOfBirth. I don't have permission to the database or the stored procedure.
Dim DOB As New DateTime
Dim newDob As String = ""
For Each x In dt.Rows
If x("DateOfBirth") <> String.Empty AndAlso IsDate(x("DateOfBirth")) Then
DOB = CDate(x("DateOfBirth"))
newDob = DOB.ToString("dd/MM/yyyy")
x("DateOfBirth") = newDob
End If
Next
You can't remove the 00:00:00 from a date value, just from a date string. You can remove it when showing a date string by ToString("dd/MM/yyyy") as you did, but x("DateOfBirth") is a date value (as written in your if statement), and it measures the ticks (10 pow -7 of a second) from 1/1/1970 (sql datetime) or 1/1/0001 (sql datetime2 or c# DateTime). A date value doesn't have any string representation, only when you convert it to a string.
A DateTime entry in your datarow is a blob of binary data in practice. The 00:00:00 is just the string representation of it. What you are doing is putting 21/1/1978 into the date field, but as far as the computer is concerned this is the same date as 21/1/1978 00:00:00 so what you are doing has no effect.
What you should be doing is adjusting the display of the date when you are outputting it to a string wherever you are viewing it. There you can use the .ToString("dd/MM/yyyy") to format it as you want.
Related
I am trying to format a date string. My function currently generates "16:00". I want it to generate "4:00 PM". Here is my code:
Dim eventTime As String
eventTime = Trim(odbcReader("StartTime").ToString)
I tried this:
eventTime = Trim(odbcReader("StartTime").ToString("hh:mm tt"))
But it returns nothing. How do I get the date string to format properly?
You can't format a String as a String. If you want to format a time then you have to start with a time, i.e. a TimeSpan value or a DateTime value. You need to convert your existing String to one of those types, then convert that to a String in the desired format.
As you want a time of day, you need to create a DateTime. A TimeSpan just represents a period of time without reference to a start or end point, so it can't be directly formatted the way you want. You would need something like this:
Dim s1 = "16:00"
Dim t = DateTime.Parse(s1)
Dim s2 = t.ToString("h:mm tt")
Note that the format specifier is "h:mm tt", "hh:mm tt". You specifically said that you expect a result without a leading zero so you wouldn't provide a format specifier that would create a leading zero.
im new to asp.net,im trying to display out my date output in terms of
MM/dd/yyyy
but it came out with
MM/dd/yyyy 12:00:00 AM
what can i do to remove the time?
my vb.net code is like this:
Dim date As String = myDataReader("date").ToString()
lbldate.Text = date
You can pass the format to ToString() method.
Dim date As String = myDataReader("date").ToString("MM/dd/yyyy")
You need to pass the format to the .ToString method, like this:
.ToString("MM/dd/yyyy")
It would also be a good idea to cast the myDataReader("date") to a DateTime object, like this:
Dim dateValue As DateTime = CType(myDataReader("date"), DateTime)
Dim date As String
If dateValue Is Not Nothing Then
date = dateValue.ToString("MM/dd/yyyy")
End If
Note - If the TryCast fails, then Nothing would be the value of dateValue, hence the If dateValue Is Not Nothing Then logic is there.
I have a database table that stores a datetime value.
I want to display that value on my asp.net textboxes, except I need to show date value in TextBox A and Time in TextBox B.
How can I split those values in VB.NET?
The idea is to use format string:
Date: dateTimeReadedFromDb.ToString("dd-MM-yyyy")
Time: dateTimeReadedFromDb.ToString("hh:mm:ss")
You can use the built in date formatting functions in VB:
Dim ThisDate as DateTime = Now
Dim TimePart as string = ThisDate.ToShortTimeString
dim DatePart as string = ThisDate.ToShortDateString
Hello
I'm trying to update a ms access database. I've got a gridview with a date that i use:
Dim tBox As TextBox = CType(gridStaff.Rows(e.RowIndex).FindControl("sDate"), TextBox)
I then want to take the value from this textbox and assign it to a date variable.
Dim test As Date = CDate(tBox.Text)
The problem I have is that now when I put test variable into my sql update query it stores the date in this format. mm/dd/yyyy instead of the dd/mm/yyyy format I want.
Ive tried different ways to format it I read online but to no success yet. Any advice would be great!
Thanks
Is your problem the conversion from tBox.Text to Date, or is it at the sql update query level? -- You should probably give the sql statement a parameter with a strict type. Converting from tBox.Text into test should probably be done in a Culture aware manner:
'String to convert and target date:
Dim dateString As String = "02/15/2011"
Dim d As Date
' Specify English/US culture when converting string:
Dim cul As Globalization.CultureInfo = Globalization.CultureInfo.GetCultureInfo("en-US")
d = Date.Parse(dateString, cul)
You could also rely on the default culture and simply let Date.Parse do the job without specifying the culture.
try using
Dim test As Date = DateTime.ParseExact(tBox.Text, "d/M/yyyy", CultureInfo.InvariantCulture)
or if your System is set up right and CultureInfo.CurrentCulture has the right date format,
Dim test As Date = DateTime.Parse(tBox.Text, CultureInfo.CurrentCulture )
instead of CDate()
Dim strTime as String = FomatDateForSave("28/12/2010")
Public Shared Function FormatDateForSave(ByVal strDate As String) As Date
FormatDateForSave = Date.ParseExact(strDate,'dd/MM/yyyy', System.Globalization.CultureInfo.InvariantCulture)
End Function
I am expecting strTime to be "12/28/2010" .... But its getting converted to "28/12/2010" ....
The thing is when the operation is performed by FormatDateForSave ... it converts it to "12/28/2010" ...
But when it is returned it is again converted to "12/28/2010"
I have set the Date for Page.Culture to be "dd/mm/yyyy" and want the value to be "mm/dd/yyyy" to be saved in DB.
You should store the date in the db as its native type, not a string. When you fetch it you should format it then.
Why don't you convert your date in sql before insert/update operation?
e.g.
SELECT convert(varchar, getdate(), 101) will give you date in mm/dd/yyyy format.
Sql Convert