Formatting the current date - vb.net

Either I just can't seem to find the right way to word the problem or it's a lot more complicated than I thought, but what is the easiest way to assign the current date to a variable and format it to a specific format? Basically, I want to do the VB.net equivalent of this VBA line:
formatted_date = Format(Date, "yyyy-m-d")
Thanks!

Dim formattedDate as String = DateTime.Now.ToString("yyyy-M-d")
Note that capital M is for month, lowercase m would get you minutes.

If by "Date" you really mean current date (and not an example variable), the VB.NET Equivalent is "Today"
Do one of these things... (whichever you like):
formatted_date = Today.ToString("yyyy-M-d")
formatted_date = String.Format("{0:yyyy-M-d}", Today)
If your "Date" was just an example variable, just replace "Today" in the above examples with your variable name.

to simply : SomeDate.ToString("yyyy-m-d")

Try this
Dim time As DateTime = DateTime.Now
Dim format As String = "MMM ddd d HH:mm yyyy"
Console.WriteLine(time.ToString(format))

Related

String value wrongly converted to datetime using vb.net

So I have this datetime value of 9.3.2016 18:56:12, by using datetime.parse, I can get the values but instead of getting '3' as the month, it takes '9' as month and '3' as day which is incorrect.
dim d1 as string = "9.3.2016 18:56:12"
dim d2 as datetime = datetime.parse(d1, CultureInfo.InvariantCulture)
I don't want to use datetime.parseExact because I'm having more than 1 value in the database.
Please help :( thank you!
Since you current culture appears to interpret dates in the way you expect you could simply do this,
Dim dateString = "9.3.2016 18:56:12"
Dim dateValue = DateTime.Parse(dateString)
By not specifying the InvariantCulture, you instruct DateTime.Parse to use the current culture which, in your case, interprets the date string correctly.
Sorry I've got the solution.
dim d2 as datetime = convert.todatetime(d1)

How to change date format vb.net 2015

I want to filter SQL-table between start date and end date, I used before string variable then I use string.format to make the format mm/dd/yyyy, I tried now in VB.net 2015 the following code:
Dim S as String
s=inputbox("Enter Start date")
S=string.format(S,"mm/dd/yyyy")
But it doesn't work, can somebody give me a solution?
You could try this for handling the input value, assuming you only need the date value as a formatted string, since your question is about formatting a date:
Dim S As String
S = InputBox("Enter Start date")
If IsDate(S) = True Then
Dim d As Date = Date.Parse(S)
S = d.ToString("mm/dd/yyyy")
Else
'Handle the non date input here
End If
But I think you should consider #Plutonix comment, since we don't know exactly how you are sending the date to perform the filtering, or how your table fields are defined.
Regards!

How do I format a date in a string variable to year date month in vb.net

I have a date in string variable strOrderEndDate which looks like this 8/14/2015.
I would like to convert it to 2015-08-14.
How do I do this in vb.net?
I tried strOrderEndDate.ToString(yyyy mmmm dd) but its not working
As the comments indicate, you first need to convert the string to a date using CDate. Then you can use the .ToString method on the new date type variable to format the output as desired:
Dim strOrderEndDate As String = "8/14/2015"
Dim datOrderEndDate As Date = CDate(strOrderEndDate)
MsgBox(datOrderEndDate.ToString("yyyy-MM-dd"))
...or as Plutonix recommends, you can use DateTime.Parse to perform the conversion. He's pretty smart so if he says it's better, then it's better.
Dim datOrderEndDate As Date = DateTime.Parse(strOrderEndDate)

Removing characters from date in visual basic

Im a complete NOOB in VB so please excuse the newbie question
Im running the following code which produces the current system date, as you can see in image below.
Dim cyear As Date
cyear = Date.Now
MsgBox(cyear)
My Question
I'm looking for a way to remove all the characters in the textbox above so that only the highlighted yellow numbers will remain. Which represents the last 2 digits of the current year.
You'll have to use a date format string, e.g.:
Dim value = String.Format("{0:yy}", DateTime.Now)
or
Dim value = DateTime.Now.ToString("yy")
Have a look at Custom Date and Time Format Strings.
Format the date before you output it:
Dim cyear As Date
cyear = Date.Now
Dim yearShort as string = cyear.ToString("yy")
MsgBox(yearShort)
For more formats, read here: http://msdn.microsoft.com/library/8kb3ddd4%28v=vs.110%29.aspx
It's best to simply pass a format to the tostring method.
DateTime.Now.toString("yy")
If that doesn't work...
DateTime.Now.toString("yyyy").Substring(2,2)
http://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx

Convert string date to show 2 year digits

i have a string which has a value of "08-06-2008". I want the result to look like "08-06-08". Is there a way to do this? I've tried CDate but that only gives me 8/06/2008 which doesnt resolve the issue.
Parse it to Date and back to string:
Dim dt As Date = Date.ParseExact("08-06-2008", "MM-dd-yyyy", CultureInfo.InvariantCulture)
Dim result As String = dt.ToString("MM-dd-yy", CultureInfo.InvariantCulture)
Since that is a normal format you could also omit the format string and use Date.Parse directly:
Dim dt As Date = Date.Parse("08-06-2008", CultureInfo.InvariantCulture)
I have used CultureInfo.InvariantCulture to avoid localization issues, normally your current culture is used in Parse/ParseExact and ToString.
See: Custom Date and Time Format Strings
Firstly, avoid CDate. It is a hangover from VB6, and is almost certainly less efficient than using the .net equivalent.
The following should give you the right answer:
string value = DateTime.ParseExact("08-06-2008", "dd-MM-yyyy").ToString("dd-MM-yy")
Note that your existing date format is ambiguous, so I've been unable to tell if you're meaning the 6th of August or the 8th of June - this is why I've used ParseExact over Parse. ParseExact is also slightly more efficient, as it tells the framework which format to use, rather than it having to guess.
Try this
Dim FormatString As String = ""
Dim SampleDate As DateTime
SampleDate = Now()
FormatString = Format(SampleDate,"dd-MM-yy")