VB.NET how to convert a string into time - vb.net

Ive been having some trouble with this for a while. I want to be able to retrieve values of time stored in my database (as type time).
I've tried:
reader.getDateTime("open_hours")
I just get an error.
I've also tried converting the string into time but i can only convert it into datetime, meaning that i have the date as well as the time.
How can i get just the time (in 24 hour format)?

you can use this link
for example:-
Dim strTime As String = "3:00 PM"
' Convert to datetime
Dim dtTime As DateTime = Convert.ToDateTime(strTime)
' Display in 24hr format
Response.Write(dtTime.ToString("HH:mm"))
example2:-
string militaryTime;
string originalTime = "3:11 PM";
DateTime dt;
if (DateTime.TryParse(originalTime, out dt))
militaryTime = dt.ToString("HH:mm");

Related

What is the date format to convert from string to date

I have below code to get StringToDate as 04/29/2019, but when use CDate function it's giving me #4/29/2019 12:00:00 AM# value. which date format i have to use to get 04/29/2019
Dim StringToDate As Date
Dim strNewDate As String = "04/29/2019"
StringToDate = CDate(strNewDate)
You could use DateTime.TryParse
Dim MyDate As DateTime = Nothing
Dim strNewDate As String = "04/29/2019"
DateTime.TryParse(strNewDate, MyDate)
Edit (misread your question)-
A DateTime holds value and not String. To get what you asking after parsing the date you would need to convert it back to a string with a format method or use the following:
MyDate.ToShortDateString
The way a date is represented is by what the region settings of your OS have setup.
For example, if you were to open the region settings in Control Panel you would find a format for Short Date which by what you're describing is probably M/d/yyyy. Inside the dropdown are other formats you can set and if you were to set it to MM/dd/yyyy you would see that your same code would now display 04/29/2019. But Don't Do This as it only affects your machine. Instead, understand that a date is only number, and you can control how it is displayed, the same way Windows just did from my example. A perfect example of this is that you could set a DateTime variable in many different ways. Here's a few:
Dim dt1 As DateTime = #2019-12-14#
Dim dt2 As DateTime = #1/08/2019#
Dim dt3 As DateTime = #03/03/2019#
Dim dt4 As DateTime = #4/4/2019#
The results would be
12/14/2019 12:00:00 AM
1/8/2019 12:00:00 AM
3/3/2019 12:00:00 AM
4/4/2019 12:00:00 AM
because that is how windows is set up to show me a DateTime as a string.
When you want to display this date to the user in a different format you can simply supply a format to the .ToString() method or use one of the DateTime string conversion methods (ie, .ToShortDateString, .ToLongDateString, etc) You can show the date to the user in a more preferable format.
Taking the last example (dt4) you could use
Console.WriteLine(dt4.ToShortDateString) 'and show 4/4/2019
Console.WriteLine(dt4.ToLongDateString) 'and show Thursday, April 4, 2019
Console.WriteLine(dt4.ToString("MM/dd/yyyy")) 'and show 04/04/2019

Convert string to date with format (VB.net)

I have a string with a date in this format. "24-11-2015" or "dd-MM-yyyy". I need to convert this to a date with this format. "2015-11-24" or "yyyy-MM-dd".
I've tried several ways, and all of them rely on my system format for this to work. If i run my program on a computer with english time format this works.
Date.ParseExact("24-11-2015", "dd-MM-yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
But if i run it on a computer running danish format i get error because there is no 24th month. How do i make the date format independent of my system time format.
Solution:
Dim dateTime As String = "24-11-2015"
Dim dt As DateTime = Convert.ToDateTime(dateTime)
Console.WriteLine("{0}-{1}-{2}", dt.Year, dt.Month.ToString("D2"), dt.Day.ToString("D2"))
Solution 2:
Dim dateTime As String = "24-11-2015"
Dim dt As DateTime = Convert.ToDateTime(dateTime)
Dim format As String = "yyyy-MM-dd"
Dim str As String = dt.ToString(format)
Console.WriteLine(str)
You can also try Formatting Date and Time for a Specific Culture.

not able to convert string date format in vb.net

I'm taking date from my csv file
Dim odateq As String = sData(0).Trim()
I am getting odateq as 9/15/2015
I want to convert this to 15/9/2015. So I wrote code like this
Dim newdate As DateTime = DateTime.ParseExact(odateq, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture)
And I'm getting an error like this :
String was not recognized as a valid DateTime.
any help is very appreciable...thanks
the code you wrote is using the wrong format; the date you have is in the format M/d/yyyy (month and day without leading zero are a guess because you did not specify it).
try with this one:
Dim newdate As DateTime = DateTime.ParseExact(odateq, "M/d/yyyy", System.Globalization.CultureInfo.CurrentCulture)
the format you set in the ParseExact was telling the function to expect a date in the format dd/MM/yyyy like 01/05/2015 but what you have is not in that format.
after you parse the input date, to get a string with format dd/MM/yyyy use:
Dim dateAsText As String = newdate.ToString("dd/MM/yyyy")

Date format from database gets wrong date in vb.net

I'm taking in data from a SQL database and now I'm trying to convert the data to a proper date variable in VB.NET but I don't get the proper format.
Data from the database:
28-FEB-14 01:00:00:0
28-FEB-14 13:00:00:0
The date I get when trying to convert it:
2028-02-14
2028-02-14 12:00:00
The code that is doing the conversion:
Dim theDate As DateTime
Dim try1 As Date
For i As Integer = 0 To batchCount - 1
If Date.TryParse(dv(i)(0), theDate) = True Then
try1 = theDate.ToUniversalTime()
End If
Would appreciate all the help I can get.
Firstly, date values in your database should be date types, so when you read them in, you read them into a Date type variable.
If you then want to format that date you can use a ToString overload
Dim theDate as DateTime = dr.item("date")
Debug.Writeline(theDate.ToString("yyyy-MM-dd")
Secondly if the date is being stored as a string field in the database (you should not do this but you may have no control over this) it should be in a specific format, so you can use ParseExact to convert this into a date:
Dim dateString As String
dateString = DateTime.ParseExact(dr.item("Date"), "dd-MMM-yy HH:mm:ss.f", CultureInfo.CurrentCulture)

yyyy-MM-dd date format not working

I can't get my date to format as yyyy-MM-dd, for instance 2013-01/15. I have tried both custom and predefined Date/Time formats. Any of these should work:
Dim myDate As Date = "01/15/2013"
myDate = myDate.ToString("o")
myDate = myDate.ToString("yyyy-MM-dd")
Format(myDate, "yyyy-MM-dd")
But they all keep myDate as "01/15/2013". What am I doing wrong?
A Date value doesn't have any format at all. When you format the date into a string and assign it back to the variable, it's parsed to a Date value again, and loses the formatting.
To keep the date in the formatted string form, you need to keep it as a string:
Dim formatted As String = myDate.ToString("yyyy-MM-dd")