Year - VBScript


Overview

The Year function in VBScript extracts the year as a four-digit integer from a given date value. It’s particularly useful for date manipulation, data parsing, and calendar-related calculations.

Syntax

Year(dateValue)

Parameters:

  • dateValue: A date value represented as a numeric value or a string in any recognized date format (e.g., “yyyy-mm-dd”).

Options/Flags

None.

Examples

  • Extract the year from a date stored as a numeric value:

    Dim dateValue = CDate("2023-01-01")
    Print Year(dateValue) ' Output: 2023
    
  • Obtain the year from a date string:

    Dim dateString = "12/24/2024"
    Print Year(dateString) ' Output: 2024
    
  • Use Year in a date calculation:

    Dim date1 = CDate("2021-12-31")
    Dim date2 = DateAdd("yy", 1, date1)
    Print Year(date2) ' Output: 2022
    

Common Issues

  • Ensure that the input date value is in a valid format. Invalid dates will return an error.

Integration

The Year function can be combined with other date-related functions, such as Month and Day, to perform complex date calculations. For instance, you could extract the year, month, and day from a date string and store them in separate variables for further processing.

  • Month
  • Day
  • CDate
  • DateAdd