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");
Related
I am calling the XmlDocument.Save() method and attempting to append the String value below to my file name. The method call throws an error and is complaining about the format.
Any help would be appreciated! \m/ \m/
Dim strTimestamp As String = "2017-08-30T08:44:40-05:00"
XmlDocument.Save("C:\Temp\FileName_" & strTimestamp & ".xml")
The issue is obviously the special characters in the file name so I am going to use the following DateTime format.
Dim strTimestamp As String = DateTime.Now.ToString("yyyy-MM-dd--hh-mm-ss").
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 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
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)
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()