Date or DateTime Object to Be independent of the Page Culture - asp.net

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

Related

How to format time string in VB.NET to AM/PM?

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.

how to convert datetime datatype to string in asp.net + vb

I want to convert datetime to string. I have declared DATE_PLACED_IN_SERVICE as datetime in SQL server database and want to convert it to string in my source code. I am getting the error: "Unable to cast object of type 'System.DateTime' to type 'System.String'"
Here my source code:
If rdmysql.IsDBNull(rdmysql.GetOrdinal("DATE_PLACED_IN_SERVICE")) = False Then
Dim ServiceDate As Date = rdmysql.GetString(rdmysql.GetOrdinal("DATE_PLACED_IN_SERVICE"))
ServiceTxt.Text = Format(ServiceDate, "yyyy-MM-dd")
End If
This column is of type datetime, so you cant use GetString but GetDateTime:
Dim ServiceDate As Date = rdmysql.GetDateTime(rdmysql.GetOrdinal("DATE_PLACED_IN_SERVICE"))
Instead of the VB6 Format function i would use .NET:
ServiceTxt.Text = ServiceDate.ToString("yyyy-MM-dd")
or simple and readable (if your culture uses - as date delimiter):
ServiceTxt.Text = ServiceDate.ToShortDateString()

displaying date in MM/dd/yyyy format using label

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.

How to remove 00:00:00 from a Date in VB.Net?

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.

Have kept format of ajaxcalendar control as "dd/mm/yyyy" and want to save in database as mm/dd/yyyy

I m using ajax calendar control to a textbox, have set its format as "dd/mm/yyyy" .
Bapur.Date = txtdate.Text;
in data access layer
cmd.Parameters.Add("#Date", SqlDbType.DateTime).Value = bapur.Date;
on saving, like above ( where Date is a string Bapur is object of businesslayer class)
in the database where datatable in database has date as datetime format.
m getting an error : cant convert string to datetime i dint get error when format was "mm/dd/yyyy" .
Basically, I want users to view date in dd/mm/yyyy but on saving i want it in
mm/dd/yyyy
Have tried a lot but not working.
here is my answer ---- https://stackoverflow.com/a/11720162/1445836 -----
You can use:
DateTime.ParseExact("yourDate","formatinWhichYouWant",culture of current string);
ex:
DateTime dt =DateTime.ParseExact("yourDate","formatinWhichYouWant",culture of current string);
string myDate = bapur.Text.Split("/");
string dateString = myDate[1] + "/" + myDate[0] + "/" + myDate[2];
got my answer
string old = txtdate.Text;
string newDate = DateTime.ParseExact(old, "dd/MM/yyyy", null).ToString("MM/dd/yyyy");
Bapur.Date = newDate.ToString();

Resources