Parsing unknown date fromat from String - asp.net

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.

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.

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

convert utc time into local time using vb.net

Using vb.net am trying to convert utc time to local time .but i think am getting the wrong result. Here i enclosed mycode.
code
Dim time As DateTime = New DateTime()
time = Date.FromFileTimeUtc(1344502618)
MsgBox(time)
getting result like this
1/1/1601 12:02:14 AM
is this result is correct?
You can just write
Dim time As Double = Convert.ToDouble(1344502618)
Dim baseDate As New Date()
Dim result As Date = baseDate.Add(TimeSpan.FromSeconds(time))
MsgBox(result)

Converting Date From TextBox To Date In 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)

Resources