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.
Related
I have two properties
Property FirstDayOfCsv As String
Property LastDayOfCsv As String
I want that properties obtains min and max values from parsed csv file. Values are days in "dd" format from date column. Now my code looks like this:
Dim csv As String() = IO.File.ReadAllLines("my.csv").Skip(1).ToArray
For Each line In csv
Dim col = line.Split(";"c)
Dim days As Date = col(2)
FirstDayOfCsv = days.ToString("dd").Min
LastDayOfCsv = days.ToString("dd").Max
Next
Unfortunately Min and Max doesn't return values, which I need and I'm stucking here. How to get them?
One option that you have is to add the values to a collection, but converting the String values to Integer values first. Then once you're finished iterating over the lines you would call Min/Max.
Dim csv = IO.File.ReadAllLines("my.csv").Skip(1).ToArray()
Dim everyday = New List(Of Integer)()
For Each line In csv
Dim col = line.Split(";"c)
Dim days = col(2)
Dim daysAsInt As Integer
If (Integer.TryParse(days, daysAsInt)) Then
everyday.Add(daysAsInt)
End If
Next
FirstDayOfCsv = everyday.Min()
LastDayOfCsv = everyday.Max()
Good day. There is the following code that subtracts from one date another:
Dim dt As New DateTime(2021, 8, 19)
Dim dt2 As New DateTime(2022, 8, 29)
MsgBox("Days Remaining : " & (dt2 - dt).Days)
tell me how to subtract format dates:
19.08.2021 14:07:16 - 24.08.2021 08:24:01
throwing away the time. I'm only interested in how to subtract dates of this format.
Date and time are entered in text boxes
You would get the Date property (documentation) of the DateTime values. Also, I would probably recommend using DateTime.Subtract (documentation) which would return a TimeSpan value.
Take a look at this example:
Dim dt = New DateTime(2021, 8, 19)
Dim dt2 = New DateTime(2022, 8, 29)
Dim daysLeft = dt2.Subtract(dt).Days
Example: https://dotnetfiddle.net/LMuWtu
You have to parse the formatted Dates then use .Date to keep only the Date part and throw away the time. Then you can substract and use TotalDays:
Dim input1 As String = "19.08.2021 14:07:16"
Dim input2 As String = "19.08.2021 14:07:16"
Dim d1 As DateTime = DateTime.Parse(input1).Date
Dim d2 As DateTime = DateTime.Parse(input1).Date
Dim days As Single = (d1 - d2).TotalDays
The formatted dates should match your Culture format. You can also use Parse overload to use another Culture : https://learn.microsoft.com/fr-fr/dotnet/api/system.datetime.parse?view=net-5.0#System_DateTime_Parse_System_String_System_IFormatProvider_
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
If I have two dates
Dim date1 As String = 01/05
Dim date2 As String = 01/07
Dim date As Integer
Date = 2
How do I go about making sure that 2 handles month changes and just general dates in this format?
You would parse the strings into DateTime values, then you can subtract them to get a TimeSpan value that is the difference. Use the Days property to get the whole days from the value:
Dim date1 As String = "01/05"
Dim date2 As String = "01/07"
Dim d1 = DateTime.ParseExact(date1, "MM'/'dd", CultureInfo.InvariantCulture)
Dim d2 = DateTime.ParseExact(date2, "MM'/'dd", CultureInfo.InvariantCulture)
Dim date As Integer = (d2 - d1).Days
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