locked
Vb.Net Day Subtraction From Date RRS feed

  • Question

  • 
    
    
    

    
    
    
    

    I want to code a program that has this requirements:- A) The Start Date Is 2005 Year B) Ending Date Will Be Today (Incrementation according to todays date ) C) Subtraction Of Days (-) From TextBox Counting From Today And Not Less Than October 24 2005 Now How Should I Do This ---------------------------------For Example:- If A User Types 420 In The Textbox So A Message Box Should PopUp Showing The Result In Date (User Enters 3 Todays Date 7/2/14 SO the Result Would Be 4/2/14) And If the Days Are More Less Than 2005 The Error Should PopUp. -------- So Please Help Me I Am Making A Software And In That I Need It.

    THanks


    • Edited by Janit Lodha Friday, February 7, 2014 3:24 PM
    Friday, February 7, 2014 10:21 AM

Answers

  • Provide Me The Full Code Please!!!!!!!!!

    Janit,

    You are going to have to show some effort on your part.  The others have already pointed you in the right direction.  Understanding how to parse a string into a date (make Textbox.Text a Date value) has nothing to do with how to subtract dates.  So the question you originally asked here (how to subtract a day from a given date) has been answered.

    Your next step should be to do some research on "how to parse a string into a date"; as stated this is all very simple stuff (Date.Parse) so it would seem that you aren't even trying to solve your own issues but rather are expecting someone else to do all of the work for you.  If that is the case then you should just hire a programmer to write the application for you.  If you really want to learn how to do it yourself then you'll have to put some effort into the actual learning.

    Everyone here will happily help you understand a concept that may be confusing you, but very few people will be willing to just hand you a solution without any regard for whether or not you understand the code being supplied.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Carl Cai Thursday, February 13, 2014 8:54 AM
    Friday, February 7, 2014 3:49 PM
  • Check into the Timespan structure.
    • Marked as answer by Carl Cai Thursday, February 13, 2014 8:49 AM
    Friday, February 7, 2014 10:50 AM
  • I am able to subtract the dates but having a problem with >=2005 is that the when i enter a value that goes more than 2005 it show me error on this line:-

    dt = dt.Adddays(-1 * CInt(TextBox1.Text))

    and the error is that:-

    ArgumentOutOfRangeException was unhandled

    And If i wish to change year to date like (2005 to 24/10/2005) How To Any Idea or Help??

    

     That error is because, you are trying to subtract more days than there has been sense the beginning of keeping track of days. For example from today you can only subtract 735270 days from the date which would bring you to 1/1/0001 as the date. If you subtract 735271 days you will get an error because, you can`t subtract another day from 1/1/0001.

     For the second part of your question if you wish to make it show the error messagebox if the date is before 10/24/2005 then you can use the CompareTo method as shown in the code below.

    I will show you the code i made but, i can tell you that if you are doing this for school work then i don`t think your teacher will believe you for a minute if you claim you wrote this code. I am just posting it so that maybe it will help you understand a few things. I still recommend that you should look at and read the msdn documents for the DateTime Structure and look at a few others like Int32.TryParse Method and the Try...Catch...Finally Statement in order to help you understand the code better.

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim TheDate As Date = Now 'Declare a Date Structure and assign it todays date
    
            Dim SubtractDays As Integer 'This will hold the days amount that we want to subtract from TheDate if the user enters a valid number
    
            If Integer.TryParse(TextBox1.Text, SubtractDays) Then 'Use TryParse to make sure the user entered a valid number in the textbox
    
                'Use a Try Catch to catch an error if the user enters a number that is more than there has been days sense 1/1/0001
                Try
                    TheDate = TheDate.AddDays(-1 * SubtractDays) 'Subtract the amount of days the user entered in the textbox
    
                    'Check to see if the date is before 10/24/2005
                    If TheDate.CompareTo(New Date(2005, 10, 24)) > 0 Then
                        MessageBox.Show("Long Date - " & TheDate.ToLongDateString & vbNewLine & "Short Date - " & TheDate.ToShortDateString)
                    Else
                        MessageBox.Show("That was Before 10/24/2005")
                    End If
    
                Catch ex As Exception
                    MessageBox.Show("There has not been that many days sense the beginning of time")
                    Exit Sub
                End Try
    
            Else
                MessageBox.Show("Invalid Number Entered")
            End If
        End Sub
    
    End Class
    

    • Edited by IronRazerz Saturday, February 8, 2014 1:18 AM
    • Marked as answer by Carl Cai Thursday, February 13, 2014 8:54 AM
    Friday, February 7, 2014 8:54 PM

All replies

  • Check into the Timespan structure.
    • Marked as answer by Carl Cai Thursday, February 13, 2014 8:49 AM
    Friday, February 7, 2014 10:50 AM
  • I Am New So I Don't Know What And How To Do So Please Can You Tell Me The Code Arranged According To My Needs.I Will Be Very Grateful If U Helped Me...
    Friday, February 7, 2014 10:54 AM
  • Janit,

    Don't expect people is doing your job here. We help and some create code for others. But that has to be beside by newbies to be a challenge, your code falls in the category that it can be build by every newbie like you but is for a more experienced one boring.

    The  code to subtract a day from a date is

    YourDateTime.AddDay(-1) 


    Success
    Cor

    Friday, February 7, 2014 11:29 AM
  • Thanks But I Already Tried This Code Like This as:-

    YourDateTime.AddDay(-"TextBox1.Text") 

    But it gives me a error :-  The String Could Not Be Passed Into Double Why???

    And I Also Want to Add a starting and ending date how to please ??

    Help Anyone I would be very happy if anyone can help!!

    Friday, February 7, 2014 11:50 AM
  • Please Help!!!!!!!
    Friday, February 7, 2014 12:48 PM
  • Please Help!!!!!!!

    Have patience some forums go on an 48 hour cycle.

    Roughly 

    YourDateTime.AddDay(-1 * CInt(TextBox1.Text)) 

    Be aware your code so such lack of knowledge that you are probably busy with homework, otherwise take first a course or try some you tube movies.

    The code above goes wrong for instance if somebody enters a space or even nothing.


    Success
    Cor

    • Proposed as answer by IronRazerz Friday, February 7, 2014 1:27 PM
    Friday, February 7, 2014 12:53 PM
  • It Helps But It is Not Suitable For Me for my required conditions!!

    Please Provide Another Code !! :(

    Friday, February 7, 2014 1:00 PM
  • It Helps But It is Not Suitable For Me for my required conditions!!

    Please Provide Another Code !! :(

     I would recommend looking threw the msdn documents on the DateTime Structure. It has many properties and methods you can choose from. I did this by declaring 1 DateTime structure and using the AddDays method to subtract the days and then use the Year property to see if the new date was greater than or equal to 2005. If it is then show a error messagebox and if it was not then show the date as a string in the messagebox. Try it and see what you can do. If you have problems then post your code so we can help you correct it.  :)

    Friday, February 7, 2014 2:02 PM
  • I dont know this thing provide me the code according to my requirements please!! or yours code because i  like it

    I am a newbie and can't understand this


    • Edited by Janit Lodha Friday, February 7, 2014 2:23 PM
    Friday, February 7, 2014 2:11 PM
  • I dont know this thing provide me the code according to my requirements please!! or yours code because i  like it

    I am a newbie and can't understand this


     I was a newbie too at one time. It took a lot of searching the net to find examples of what i wanted to do and trying to make them work the way i wanted. If there was something i did not understand then i searched the net for information i could read to help myself understand it.

     Anyways, here are 2 links that you should be able to get the idea of declaring a Date Structure and assigning it the current date.

    Substracting from Today

    subtracting a date with AddDate in VB.NET

    After you have the date declared and assigned the current date to it then you can use the Year property to check what year the date is after subtracting the days. For example if you have a Date structure named TheDate that you subtracted the days from already then you check the year like this

                If TheDate.Year >= 2005 Then
                    MessageBox.Show("Long Date - " & TheDate.ToLongDateString & vbNewLine & "Short Date - " & TheDate.ToShortDateString)
                Else
                    MessageBox.Show("That was Before 2005")
                End If
    

    There are several ways to show the date as a string but, i used the .ToLongDateString and .ToShortDateString for the above example.

    Friday, February 7, 2014 2:50 PM
  • Provide Me The Full Code Please!!!!!!!!!
    Friday, February 7, 2014 2:54 PM
  • Edit:- The Requirements A) Is to be changed:-

    The Start Date Would Be October 24,2005

    Friday, February 7, 2014 3:00 PM
  • I see you use a woman name, be aware it helps sometimes. Some men think they have to be a little bit more graceful. 

    However, it is often just the same of an old trick.

    Be aware nobody has to give you code. If you real want it, go to your teacher.


    Success
    Cor

    • Proposed as answer by Carl Cai Thursday, February 13, 2014 8:56 AM
    Friday, February 7, 2014 3:01 PM
  • Provide Me The Full Code Please!!!!!!!!!

    Janit,

    You are going to have to show some effort on your part.  The others have already pointed you in the right direction.  Understanding how to parse a string into a date (make Textbox.Text a Date value) has nothing to do with how to subtract dates.  So the question you originally asked here (how to subtract a day from a given date) has been answered.

    Your next step should be to do some research on "how to parse a string into a date"; as stated this is all very simple stuff (Date.Parse) so it would seem that you aren't even trying to solve your own issues but rather are expecting someone else to do all of the work for you.  If that is the case then you should just hire a programmer to write the application for you.  If you really want to learn how to do it yourself then you'll have to put some effort into the actual learning.

    Everyone here will happily help you understand a concept that may be confusing you, but very few people will be willing to just hand you a solution without any regard for whether or not you understand the code being supplied.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Carl Cai Thursday, February 13, 2014 8:54 AM
    Friday, February 7, 2014 3:49 PM
  • I am able to subtract the dates but having a problem with >=2005 is that the when i enter a value that goes more than 2005 it show me error on this line:-

    dt = dt.Adddays(-1 * CInt(TextBox1.Text))

    and the error is that:-

    ArgumentOutOfRangeException was unhandled

    And If i wish to change year to date like (2005 to 24/10/2005) How To Any Idea or Help??

    
    Friday, February 7, 2014 4:27 PM
  • I am able to subtract the dates but having a problem with >=2005 is that the when i enter a value that goes more than 2005 it show me error on this line:-

    dt = dt.Adddays(-1 * CInt(TextBox1.Text))

    and the error is that:-

    ArgumentOutOfRangeException was unhandled

    And If i wish to change year to date like (2005 to 24/10/2005) How To Any Idea or Help??

    

     That error is because, you are trying to subtract more days than there has been sense the beginning of keeping track of days. For example from today you can only subtract 735270 days from the date which would bring you to 1/1/0001 as the date. If you subtract 735271 days you will get an error because, you can`t subtract another day from 1/1/0001.

     For the second part of your question if you wish to make it show the error messagebox if the date is before 10/24/2005 then you can use the CompareTo method as shown in the code below.

    I will show you the code i made but, i can tell you that if you are doing this for school work then i don`t think your teacher will believe you for a minute if you claim you wrote this code. I am just posting it so that maybe it will help you understand a few things. I still recommend that you should look at and read the msdn documents for the DateTime Structure and look at a few others like Int32.TryParse Method and the Try...Catch...Finally Statement in order to help you understand the code better.

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim TheDate As Date = Now 'Declare a Date Structure and assign it todays date
    
            Dim SubtractDays As Integer 'This will hold the days amount that we want to subtract from TheDate if the user enters a valid number
    
            If Integer.TryParse(TextBox1.Text, SubtractDays) Then 'Use TryParse to make sure the user entered a valid number in the textbox
    
                'Use a Try Catch to catch an error if the user enters a number that is more than there has been days sense 1/1/0001
                Try
                    TheDate = TheDate.AddDays(-1 * SubtractDays) 'Subtract the amount of days the user entered in the textbox
    
                    'Check to see if the date is before 10/24/2005
                    If TheDate.CompareTo(New Date(2005, 10, 24)) > 0 Then
                        MessageBox.Show("Long Date - " & TheDate.ToLongDateString & vbNewLine & "Short Date - " & TheDate.ToShortDateString)
                    Else
                        MessageBox.Show("That was Before 10/24/2005")
                    End If
    
                Catch ex As Exception
                    MessageBox.Show("There has not been that many days sense the beginning of time")
                    Exit Sub
                End Try
    
            Else
                MessageBox.Show("Invalid Number Entered")
            End If
        End Sub
    
    End Class
    

    • Edited by IronRazerz Saturday, February 8, 2014 1:18 AM
    • Marked as answer by Carl Cai Thursday, February 13, 2014 8:54 AM
    Friday, February 7, 2014 8:54 PM
  • As Cor Ligthert Said ...

    Insist Yourself Rather Others ...

    what you need in my view is the mathematical way of adding and subtracting ..

    Dim intDate as integer = DateTime.ParseExact(strToDay, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToOADate

    Dim intBegngDate as integer = DateTime.ParseExact("24/10/2005", "dd/MM/yyyy", CultureInfo.InvariantCulture).ToOADate

    if (intdate - cint(Text1.text)) < intBegngDate then

       Message For Date Less Than 24/10/2014

    else

        Message "User Entered " &  Format(Date.FromOADate((intdate - cint(Text1.text))), "dd/MM/yyyy")

    endif

    Basically convert date to integer so that u can make arithmetical operation like a child as like Me .

    Friday, February 7, 2014 10:45 PM
  • Basically convert date to integer so that u can make arithmetical operation like a child as like Me .

    Dates can be compared directly.  There is no need to convert a date to a different format in order to carry out the comparison.
    Saturday, February 8, 2014 12:41 AM
  • Dates can be compared directly.  There is no need to convert a date to a different format in order to carry out the comparison.

     Thanks for bringing that up. I always seem to forget you can use the Compare and CompareTo methods for comparing 2 DateTime structures. I fixed my code above to use CompareTo.   :)
    Saturday, February 8, 2014 1:23 AM