Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have three text box like year month and day and enter like 28 year 2 month as nd 3 days so find my date fo birth date in vb.net
Posted
Comments
Maciej Los 20-Apr-15 14:04pm    
What have yo tried? Where are you stuck?

I guess you are probably looking for a solution to find the birthday, and you can do so by adding the values of years, months and days (in a negative form as to subtract them).

I used the following C# code to get my date of birth, it writes it perfectly.

C#
using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine(
            DateTime.Now.AddYears(-19).AddMonths(-8).AddDays(9)
            .ToString("MMMM dd, yyyy on dddd")
        );
    }
}

// Output: August 29, 1995 on Tuesday


Since you want to get it in VB, so the code would be...

VB
Public Class Program
    Public Shared Sub Main()
        Console.WriteLine(
            DateTime.Now.AddYears(-19)
            .AddMonths(-8).AddDays(9)
            .ToString("MMMM dd, yyyy on dddd")
        )
    End Sub
End Class


Remember that you are to replace the constant added values as variables. So your code might be like this,

VB
Public Class Program
    Public Shared Sub Main()
        Dim year As Integer, month As Integer, day As Integer
        Console.WriteLine(
            DateTime.Now.AddYears(year)
            .AddMonths(month).AddDays(day)
            .ToString("MMMM dd, yyyy on dddd")
        )
    End Sub
End Class


Then pass the values as negative or positive depending on current date or month. It would give you with the required result that you want to get.
 
Share this answer
 
In addition to solution 1, here covering the input-aspect:

I would suggest you to use a DateTimePicker-control instead of three TextBoxes. The DateTimePicker would spare you to parse the input from the TextBoxes and to convert the values into a DateTime (where the entered values could be invalid for representing a date). It would also be more convenient for the user.

Normally, for parsing an integer from text I would suggest to use Int32.TryParse(..) because that avoids having to catch a potential exception. But since you would then also need to construct a DateTime-object from the year, month and day-values and there's no easy way to do this while avoiding a potential exception, I would do it like this (if I didn't want to use a DateTimePicker for whatever reason):

VB
Dim dayBefore As DateTime

Try
    Dim year As Integer = Int32.Parse(YearInput.Text)
    Dim month As Integer = Int32.Parse(MonthInput.Text)
    Dim day As Integer = Int32.Parse(DayInput.Text)

    Dim enteredDate As New DateTime(year, month, day)
    Dim oneDay As New TimeSpan(1, 0, 0, 0)
    dayBefore = enteredDate - oneDay

Catch ex As Exception
    dayBefore = DateTime.MinValue
End Try

If dayBefore <> DateTime.MinValue Then
    ' do something here
Else
    ' report input error to user
End If
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900