Converting Date From TextBox To Date In ASP.Net - asp.net

I want to get the difference of date from Two TextBox say to and from dates..
My Code is here
Dim ts As TimeSpan
ts = CDate(txtTo.Text) - CDate(txtFrom.Text)
txtDays.Text = ts.Days() + 1
but this code is throwing an error like this
Conversion from string "16/11/2011" to type 'Date' is not valid.

In general, try to avoid using old VB functions like CDate. In this case, you should use the Parse method:
Dim ts As TimeSpan
ts = DateTime.Parse(txtTo.Text) - DateTime.Parse(txtFrom.Text)
Or, if you know the date format in advance:
Dim fmt As String = "dd/MM/yyyy"
Dim ts As TimeSpan
ts = DateTime.ParseExact(txtTo.Text, fmt, Nothing) - DateTime.ParseExact(txtFrom.Text, fmt, Nothing)

Try using ParseExact like:
Dim s As String = txtFrom.Text.trim
Dim pattern As String = "dd/MM/yyyy"
Dim parsedDate As Date = Date.ParseExact(s, pattern, Nothing)
As that way you can specify the format of the string that is used.
More direct to your example:
Dim pattern As String = "dd/MM/yyyy"
ts = Date.ParseExact(txtTo.Text, pattern, Nothing) - Date.ParseExact(txtFrom.Text, pattern, Nothing)

You can directly cast a string to DateTime. You need to use the DateTime.Parse function to parse a string to a DateTime.
The syntax can be found here.

Use overloaded version of the function:
DateTime value = DateTime.Parse(txtFrom.Text, new CultureInfo("gu-IN", false));
where "gu-IN" is for Gujarati (India)

Related

Parsing unknown date fromat from String

Is there a way to programmatically find the Date/Time format of a string in Visual Basic?
My example comes from work. I was working on an Custom Web Page to compare tables on Excel Spreadsheets . My comparison of the records on each table is based off of last four digits of social security and birthdate, however currently the format of the birthdate has to be the same on both spreadsheets for it to run properly. The user wanted to compare early before we had updated the reports for this project, and I told her to make sure the birthday columns were in the same datetime format before she ran the web page. She did not.
I have two options, hard code the date time format that currently exists on the spreadsheets, or find a way to make the format irrelevant. I think it would be more fun to do the latter.
I know that there is a Date.ParseExact method, but that requires that you know the format of the string already.
To give you a better idea of what I want to do:
Dim Birthday1 As String = "04/15/1998 13:00:00"
Dim Birthday2 As String = "04/15/1998 1:00 PM"
'//Converts to Date in format "M/d/yyyy"
Dim dateBirthday1 As Date = ConvertFromAnyDateFormat(Birthday1)
Dim dateBirthday2 As Date = ConvertFromAnyDateFormat(Birthday2)
If dateBirthday1 = dateBirthday2 Then
'//Do comparison operations or whatever
End If
In your case, the strings are in good format. So just allow the system to cast the values
This will work for your two dates.
Imports System.Globalization
Public Class MyFunExample
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Birthday1 As String = "04/15/1998 13:00:00"
Dim Birthday2 As String = "04/15/1998 1:00 PM"
Dim Birthday3 As String = "04/15/98 1:00 PM"
Dim Birthday4 As String = "04/15/98"
'//Converts to Date in format "M/d/yyyy"
Dim dtBir1 As DateTime = Birthday1
Dim dtBir2 As DateTime = Birthday2
Dim dtBir3 As DateTime = MyDateCheck(Birthday3)
Dim dtBir4 As DateTime = MyDateCheck(Birthday4)
Debug.Print(dtBir1)
Debug.Print(dtBir2)
Debug.Print(dtBir3)
Debug.Print(dtBir4)
if dtBir1 = dtBir2 then
End if
End Sub
Function MyDateCheck(sDate As String) As DateTime
If IsDate(sDate) Then
Return sDate
End If
Dim rFoundOne As DateTime
' if we get here, then lets try some other formats
If DateTime.TryParse(sDate, CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite, rFoundOne) Then
Return rFoundOne
End If
' try additonal messy cases
If DateTime.TryParseExact(sDate, "mm dd yy", CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite, rFoundOne) Then
Return rFoundOne
End If
' if all fail, then we return a "0" date
Return DateTime.MinValue
End Function
Output:
1998-04-15 1:00:00 PM
1998-04-15 1:00:00 PM
1998-04-15 1:00:00 PM
1998-04-15
So if things are messed up - then you could introduce the date check routine, but in your case you just need to shove the strings into a datetime, and thus they are then considered NOT a string, but a internal date format number, and thus the compares will work.

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.

UTC to datetime conversion

I have the following date time string:
2013-08-16T09:21:05-04:00
How can I get that into a local datetime format in VB.NET?
UPDATE
Just realised the string had some extra quotes on each side that hadn't been sanitized properly #blush
Look at the DateTime.ParseExact() and DateTime.TryParse() methods.
http://msdn.microsoft.com/en-us/library/System.DateTime_methods.aspx
Have you tried something like this
Dim s As String = "2013-08-16T09:21:05-04:00"
Dim d As DateTime = DateTime.Parse(s)
Dim dateValue As DateTime = DateTime.Parse("2013-08-16T09:21:05-04:00")
Debug.WriteLine(dateValue.ToString)
Output on my machine (British English, GMT+1):
16/08/2013 14:21:05

String / DateTime Conversion problem (asp.net vb)

I have this code:
Dim birthdaystring As String = MonthBirth.SelectedValue.ToString & "/" & DayBirth.SelectedValue.ToString & "/" & YearBirth.SelectedValue.ToString
Dim birthday As DateTime = Convert.ToDateTime(birthdaystring)
Which produces errors (String was not recognized as a valid DateTime.)
The string was "01/31/1963".
Any assistance would be appreciated.
Thanks.
The problem is probably that the culture used for parsing the date expects a MM/dd/yyyy format rather than dd/MM/yyyy format.
Instead of creating a string and parsing it, create the DateTime value directly:
Dim birthday As New DateTime(YearBirth.SelectedValue, MonthBirth.SelectedValue, DayBirth.SelectedValue)
Instead of creating a string which you later try to parse to a date try this:
Dim birthday As DateTime = new DateTime(_
CType(YearBirth.SelectedValue, Integer), _
CType(MonthBirth.SelectedValue, Integer), _
CType(DayBirth.SelectedValue, Integer))
Try changing it to:
DateTime.Parse(birthdaystring);
I'm guessing it will work, but if not - you can add a second parameter to the parse saying the format you input, i.e.:
DateTime.Parse(birthdaystring,"MM/dd/yyyy");

Resources