yyyy-MM-dd date format not working - vb.net

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")

Related

VB.NET: Convert a date dd/mm/yy to dd/mm/yyyy

I need to convert a dd/mm/yy date to dd/mm/yyyy automatically.
Example: if I insert in the textbox "12/01/90", the program should automatically convert the text to "12/01/1990" or "01/01/20" to "01/01/2020".
Also, I should check the date to make sure it is correct.
Example: if I enter "80/70/2000" it must give me an error because the date does not exist. How can I do? Thanks in advance.
If you have the input as a string, you would first parse it into a date with the format specifier, and then convert it back into a string.
Dim input as String = "22/03/19" ' dd/mm/yy
Dim dt as DateTime = DateTime.MinValue
If (DateTime.TryParseExact(input,
"dd/MM/yy",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
dt
)) Then
Dim output as String = dt.ToString("dd/MM/yyyy")
Console.WriteLine(output)
else
Console.WriteLine("Error")
end if
Output:
22/03/2019
You can try it here

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)

How translate string to date in .NET

I followed suggestion from vb.net convert string to date . But, it did not work.
the code is as follows:
Dim Dt As DateTime
If DateTime.TryParse("Thu, 09 Dec 2010 16:03:24 EST", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, Dt) Then
MessageBox.Show(Dt)
End If
Can anyone solve this for me? I need to have date populated in the format of "yyyy-mm-dd hh24-mi-ss".
this should get you on the right path:
Dim dt As DateTime
dt = Now
TextBox1.Text = Format(dt, "yyyy-MM-dd HH:mm:ss")
NOTE: this answer was written for a previous revision of the question which had the following code:
Dim Dt As DateTime
If DateTime.TryParseExact("Thu, 09 Dec 2010 16:03:24 EST", "dd.MM.yyyy", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, Dt) Then
MessageBox.Show(Dt)
End If
The format string that TryParseExact takes as it's second parameter specifies the format of the date in the string passed as the first.
In your case the format string is specifying that the date will be of the format "09.12.2010" for example - just the day, moth and year. However, as the string isn't in that format it won't parse. If you'd just used ParseExact it would have raised an exception.
The MSDN page for the variant of TryParseExact that takes an array of possible format strings has more examples, but non match your format exactly, but working with the format strings used to convert DateTime to string you probably want something like this:
"ddd, dd MMM yyyy HH:mm:ss ???"
but I can't find what you'd need instead of "???" to match the "time zone as string". You might have to do some string manipulation to remove this before calling TryParse or TryParseExact.
You will have to replace the timezone with the timezone offset.
Same question as Parse DateTime with time zone of form PST/CEST/UTC/etc
TextBox1.Text = System.DateTime.Now.AddMinutes(698).ToString("dd/MM/yyyy");
DateTime DOB;
string[] formatsDOB = { "dd/MM/yyyy", "MM/dd/yyyy" };
DateTime.TryParseExact(txtDateofBirth.Text, formatsDOB, CultureInfo.CurrentCulture, DateTimeStyles.None, out DOB);