DATE concept in VB.NET - vb.net

i have created VB.net project.In that i have two textbox,and two buttons
button1-->submit
button2-->Duedate
textbox1 contain the current date
My constraints is if i click button2(Duedate) than add 30 days to textbox1 date and assign that value into textbox2.
How to achieve this?
I want the result like as folloes
If I give textbox1 = 12/12/2009
than
I click Duedate: textbox2.text =11/1/2010
Is it possible.
Thanks in advance.

Like so:
Dim d As Date
If DateTime.TryParse(textbox1.Text, d) Then
textbox2.Text = d.AddDays(30).ToShortDateString()
End If

Your button text should be something like:
If IsDate(TextBox1.Text) Then
Dim newdate As Date = CDate(TextBox1.Text)
newdate = newdate.AddDays(30)
Dim myDateFormat As String = "dd/MM/yyyy" //or whatever
DueDateTExtbox2.Text = newdate.ToString(myDateFormat)
End If

'test data
Dim dStr As String = "25/12/2009"
Dim d, d30 As DateTime
Dim invC As New System.Globalization.CultureInfo("") 'invariant culture
If DateTime.TryParseExact(dStr, "dd/M/yyyy", _
invC, _
Globalization.DateTimeStyles.AllowWhiteSpaces _
Or Globalization.DateTimeStyles.AssumeLocal, d) Then
d30 = d.AddDays(30)
End If

Related

Loop through textboxes with datetime

I have this code to add days to the first day of the month.Could someone help me to write this shorter by looping through the Textboxes and add to each box an extra day?
Dim dpt As DateTimePicker = DateTimePicker1
Dim day1 As DateTime = dpt.Value.ToString("01/MM/yyyy")
Dim textBoxes() As TextBox = {TextBox187, TextBox188, TextBox189, TextBox190, TextBox191,TextBox192, TextBox193, TextBox194, TextBox195, TextBox196, TextBox197, TextBox198, TextBox199, TextBox200, TextBox201, TextBox202, TextBox203, TextBox204, TextBox205, TextBox206, TextBox207, TextBox208, TextBox209, TextBox210, TextBox211, TextBox212, TextBox213, TextBox214, TextBox215, TextBox216, TextBox217}
textBoxes(0).Text = day1
textBoxes(1).Text = day1.AddDays(1)
textBoxes(2).Text = day1.AddDays(2)
textBoxes(3).Text = day1.AddDays(3) etc..
Thanks
For loop should fit here:
For i = 0 To textBoxes.Length - 1
textBoxes(i).Text = day1.AddDays(i)
Next

Entering Date in Excel Sheet in VB. Date format changes if day is entered less than 12

This is my Code :
'HomeP form has the excelapp, excelwb, excelws objects initialized.
HomeP.excelws.Range("C1", "C900000").NumberFormat = "dd-MMM-yyyy"
HomeP.excelws.Range("D1", "D900000").NumberFormat = "dd-MMM-yyyy"
I'm taking input using a textbox.
HomeP.excelws.Cells(2, 3).Value = TextBox3.Text
HomeP.excelws.Cells(2, 4).Value = TextBox4.Text
[EDIT] : Now the issue is, if I enter '23-12-2001' then it will store as '23-Dec-2001' in excel but if I enter '01-11-2001' then it will store it as '11-Jan-2001'. I don't think it's about the input but how Excel interprets it when the day is less than 12. My system date format is dd-mm-yyyy. I want to have date format in excel cell as 'dd-MMM-yyyy'
Thanks to TnTinMin and Aladein, The problem has been solved. I've created this method to get the date in my required format.
Public Function GetDateProperly(ByVal tb As TextBox)
Try
Dim timee As DateTime = tb.Text
Dim Formatt As String = "dd-MMM-yyyy"
Dim newdate = timee.ToString(Formatt)
Return CDate(newdate)
Catch ex As System.InvalidCastException
Catch ex As NullReferenceException
End Try
End Function
i think you can try :
Dim Timee As DateTime = HomeP.excelws.Cells(2, 3).Value
Dim Formatt As String = "dd-MM-yyyy"
Dim newdate = Timee.ToString(Formatt)
TextBox3.Text = newdate
and please tell me if it work !!

How to convert textbox year date value to check if it has only 2 digits?

The problem that I have is related to only being possible to insert 2 digit's on the field year and I was wondering if it was possible to have a string with a value 2017 and only inserting 17 when I press a button.
I have tried to use the CONTAINS property to check if it only has numbers, but now I'm kind of stuck.
You could use regex.
Private Function returnLastTwo(input As Integer) As Integer
Dim matchYearString As String = "(?:(\d\d)(?:(\d\d))?)"
Dim output As Integer
If CStr(input).Length = 4 Then
output = Text.RegularExpressions.Regex.Match(CStr(input), matchYearString).Groups(2).Value
ElseIf CStr(input).Length = 2 Then
output = Text.RegularExpressions.Regex.Match(CStr(input), matchYearString).Groups(1).Value
Else
Throw New Exception("Check input")
End If
Return output
End Function
You can use a string/substring concept;
Module DateSplit
Sub Main()
Dim literal As String = "2017"
Dim substring As String = literal.Substring(2)
Console.WriteLine("Substring: {0}", substring)
End Sub
End Module
I did not test this code but should output 17
Another approach would be casting your date as a string and create a char array, then you can have access to the element and convert them back to numbers if you like
Dim dateToSplit As Integer = 2017
Dim strDate As String = dateToSplit.ToString()
Dim charArr() AS Char = strDate.ToCharArray()
Assuming your user enters a date like this: 01/01/2017, into the textbox:
dim dateFromTextbox = yourTextbox.Text ' ==> 01/01/2017
dim lastTwoDigits = Right(dateFromTextbox, 2) ' ==> 17

How to Convert Integer to Date?

Is it possible to convert integer to date? I am using a datagridview where users can type a combinations of number (e.g 07191993) when in editmode then after the user is done editing, the program should format it in date (07-19-1993).
To get a date from the number inputed by user :
Dim theDate = DateTime.ParseExact (input, "MMddyyyy", CultureInfo.InvariantCulture) ' or some other specific CultureInfo / FormatProvider)
To display the date as wanted (if given FormatProvider doesn't do it by itself) :
Dim repr = theDate.ToString ("MM-dd-yyyy")
Create an event for CellEndEdit of datagridview and put in this code
Dim n As Integer = 'Datagridview cell value'
Dim mon As Integer = Convert.ToInt32(n.ToString.Substring(0, 1))
Dim dt As Integer = Convert.ToInt32(n.ToString.Substring(1, 2))
Dim yr As Integer = Convert.ToInt32(n.ToString.Substring(3, 4))
Dim dt1 As New DateTime(yr, mon, dt)
hope this helps.

get the sorted date in vb

i am writing simple program to get the sorted date . But it does not work.
Dim filepath As String = FileStr
Dim directoryPath As String = System.IO.Path.GetDirectoryName(filepath)
for Each file As String In System.IO.Directory.GetFiles(directoryPath)
Dates = {
Date.Parse(System.IO.Path.GetFileNameWithoutExtension(file))
}.ToList
Next
Dates.Sort()
ComboBox1.DataSource = Dates
It only show one date ..where there are more than 10 date. and also the loop is working
I declare the List as global
You're replacing the content of your list in every loop
Dim filepath As String = FileStr
Dim directoryPath As String = System.IO.Path.GetDirectoryName(filepath)
for Each file As String In System.IO.Directory.GetFiles(directoryPath)
Dates.Add(Date.Parse(System.IO.Path.GetFileNameWithoutExtension(file)))
Next
Dates.Sort(AddressOf SortDate)
ComboBox1.DataSource = Dates
UPDATE: Sorting issue
Then as a seperate function add:
Function SortDate(ByVal a As DateTime, ByVal b As DateTime)
Dim result As Integer = a.Year.CompareTo(b.Year)
If result = 0 Then
a.Month.CompareTo(b.Month)
If result = 0 Then
a.Day.CompareTo(b.Day)
End If
End If
Return result
End Function
Your loop replaced the value of Dates each time, so you get only one date.
Change the loop like this:
Dates = System.IO.Directory.GetFiles(directoryPath)
.Select(Function(file) Date.Parse(System.IO.Path.GetFileNameWithoutExtension(file)))
.ToList