0

I want to be able to subtract only 'yyyy' in my script. For example, starting with the year 2014, I want to be able to subtract the current year (right now being 2019, next year being 2020, and so on). With 2019 being the current year, I would like to get 5 as my output.

Here is what I have tried so far:

        Module Test
        Public Module DateAndTime
          Sub Main()
            Dim startYear As Int = 2014
            Dim currentYear As Date = #2019#
            currentYear As Int = 2019
            mfgYear As Int = currentYear - startYear 
            Console.WriteLine(mfgYear)
          End Sub
        End Module
        End Module

I am an extremely new person to vb.net, so I apologize for the poor attempt at the code above. Any help would be greatly appreciated.

Bman
  • 11
  • 3
  • 1
    https://stackoverflow.com/questions/20635037/get-date-difference-in-vb-net – Honeyboy Wilson Dec 02 '19 at 16:04
  • I don't think you tried very hard. If you typed this into a Visual Studio code window you would see red squiggles all over. For starters `Dim startYear As Int` Do you have a class called Int in your project? – Mary Dec 03 '19 at 03:37
  • You can't have a variable `currentYear As Date` and `currentYear As Integer` in the same scope. I presume you mean Integer not Int. You can't change the datatype of a variable with an As clause. – Mary Dec 03 '19 at 03:42

4 Answers4

2

You can get the current year, as an integer, like this:

Dim currentYear As Integer = Date.Now.Year
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
1

This might help you:

    Dim date1 As Date : Date.TryParse("11/02/2014", date1)
    Dim date2 As Date : Date.TryParse("02/12/2019", date2)

    Dim years As Integer =  CInt(DateAndTime.DateDiff(DateInterval.Year, date1, date2))
    Dim days As Long = DateAndTime.DateDiff(DateInterval.Day, date1, date2)
    Dim hours As Long = DateAndTime.DateDiff(DateInterval.Hour, date1, date2)

    Console.WriteLine("From start date has passed " &
          years.ToString & " years    OR " &
          days.ToString & "  days    OR " &
          hours.ToString & " hours ")
G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16
0

Thank you for the help. I found some additional stuff online and was able to get it working with the following:

```
Dim startDate As Date
Dim startYear As Integer
startDate = #1/1/2014#
startYear = Year(startDate)

Dim currentDate As Date
Dim currentYear As Integer
currentDate = Today
currentYear = Year(Today)

Dim mfgYear As Integer
mfgYear = currentYear - startYear
```

I got my desired value of '5', and will get '6' when it reaches 2020 and so on.

Bman
  • 11
  • 3
0

Try this:

Module Test
        Public Module DateAndTime
          Sub Main()
            Dim startYear As Int = 2014
            Dim currentYear As Date = DateTime.Now.Year
            mfgYear As Int = currentYear - startYear 
            ' result for mfgYear = 5
            Console.WriteLine(mfgYear)
          End Sub
        End Module

If you want just the current year you can use: Dim DateNow As Integer = DateTime.Now.Year

Vladut
  • 647
  • 1
  • 10
  • 35