How to change date format vb.net 2015 - vb.net

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!

Related

VB.NET - Unable to enforce two digit day and month when converting string to Date

I am having difficulty taking a string and converting it to a vb.net Date object, while enforcing two digit day and month. Please consider the following form example, using today's date. (02/01/2019)
Dim myDate As Date = Date.Now
Dim myDateString = String.Format("{0:D2}/{1:D2}/{2:D4}", myDate.Month, myDate.Day, myDate.Year)
myDate = DateTime.ParseExact(myDateString, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None)
Label1.Text = myDate 'This will show "2/1/2019"
Label2.Text = myDateString 'This will show "02/01/2019"
This situation leaves Label1.Text as "2/1/2019", but Label2.Text as "02/01/2019". No matter what I have tried, it appears that the actual conversion from the correctly formatted String into a Date object will remove these zeros. Does anyone have any thoughts as to how I can enforce a "MM/dd/yyyy" format when converting to a Date object?
Thank you in advance,
You should consider that a DateTime variable has no format. It is just a number expressing the Ticks elapsed from the starting DateTime.MinValue (1/1/0001).
It has no memory that you have built it using a particular formatting parser.
So, when you assign a date to a string like you do in your
Label1.Text = myDate
then you are asking the ToString representation of that date. The output of this method without a formatting parameter is whatever your locale settings decide it to be. If you want to force the desidered output you need to tell the date's ToString method in what format you want the output
Label1.Text = myDate.ToString("MM/dd/yyyy")

How do i add 1 month to a date that already has been created on VB?

Hi I'm new to coding and am looking for some help! I've been trying to add 1 month to a date that I've already stored in my database and displayed on a gridview.
Dim dueDate As DateTime = lblDateA.Text.AddMonth(1)
I know the way that I've done it is wrong but i hope you get the idea i'm going for! Thanks in advance!
First convert the date to a DateTime and add Month to it.
Dim dueDate As DateTime = Convert.ToDateTime(lblDateA.Text).AddMonths(1)
Firstly, the Text of a Label is a String, so you can't add a month to it. Whatever you're displaying in that Label should already be being stored in a DateTime variable somewhere. You would then add one month to that and then display the new value, e.g.
Me.dateA = Me.dateA.AddMonths(1)
lblDateA.Text = Me.dateA.ToShortDateString()
You convert the text to a Date and then add the month.
Private Function ConvertDGVDateAndAddMonth(dateAsString As String) As Date
Dim dt As Date
If Date.TryParse(dateAsString, dt) Then
'parsed correctly so we can use the dt variable as a date
Return dt.AddMonth(1)
End If
'if it does not convert return Nothing
Return Nothing
End Function
USage:
Dim dt As Date = ConvertDGVDate(dgv.CurrentCell.Value.ToString)
'if this does not convert dt = Nothing
'be sure to check

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

Sharepoint 2010 dates and calculated fields

For reasons I don't pretend to understand calculated fields that are set to return a date return the value in code in this format:
"datetime;#2015-04-25 00:00:00"
so dim myDate as datetime = oMasterItem("Contract End Date") fails, and you can't cast the value either.
How do I convert that to a real date format without doing string manipulation ?
(or am I missing something obvious?)
Many thanks!
Check this out:
SharePoint - get value of calculated field without manual parsing
You can do it by:
SPFieldCalculated cf = (SPFieldCalculated)myItem.Fields["CIDandTitle"];
string value = cf.GetFieldValueForEdit(myItem["CIDandTitle"]);
or
string value = cf.GetFieldValueAsText(myItem["CIDandTitle"]);

How to get a date field from MM/dd/yyyy to yyyy/MM/dd in vb.net

I need to get a date field from MM/dd/yyyy to yyyy/MM/dd in vb.net but it should still be a date field afterward so that I can match it against a date in a database.
At the moment all I'm managing to do is to change it to a string in that format.
I tried this type of code which also did not work.
DateTime.Parse(yourDataAsAString).ToString("yyyy-MM-dd")
fromDeString = String.Format("{0:yyyy/MM/dd}", aDate)
fromDate = Format("{0:yyyy/MM/dd}", aDate)
Any help would be much apreciated, thanks
You're not understanding that a date object does not store the digits in any particular format. The only way to get the digits formatted in the order you want is to convert it to a string. Why do you need to compare them in a particular format? A date is a date no matter how it is formatted. 12/15/78 == 1978/12/15.
If you are not able to compare dates from the DB against a date object in VB, it is likely that the date you are comparing to in the database is being returned to you in string format, in which case you should covert it to a date object for comparison.
Dim sDate As String = "2009/12/15" 'Get the date from the database and store it as a string
Dim dDate As New Date(2009, 12, 15) 'Your date object, set to whatever date you want to compare against
Select Case Date.Compare(dDate, Date.Parse(sDate))
Case 0
'The dates are equal
Case Is > 0
'The date in the database is less
Case Is < 0
'The date in the database is greater
End Select
Here's a sample module demonstrating the functionality you desire.
Imports System.Globalization
Module Module1
Sub Main()
Dim culture As New CultureInfo("en-us", True)
Dim mmDDyy As String = "10/23/2009"
Dim realDate As Date = Date.ParseExact(mmDDyy, "mm/dd/yyyy", culture)
Dim yyMMdd As String = realDate.ToString("yyyy/MM/dd")
End Sub
End Module
Hope this helps.
Kind Regards
Noel
Your second should actually work. Instead just try:
dim chislo as date = date.now dim message As String = $"
Today`s Date: {String.Format("{0:dddd, dd/MM/yyyy}", Chislo)} "
MsgBox(message)