What It Does

The getYearValue() method extracts the year portion from a GlideDateTime object and returns it as a four-digit number. The method operates on the UTC representation of the datetime, ignoring any user timezone settings or system timezone configuration. When you call this method, ServiceNow examines the internal UTC timestamp stored in the GlideDateTime object and returns only the year component.

Internally, ServiceNow maintains GlideDateTime objects as UTC timestamps. When getYearValue() executes, it parses this UTC timestamp and extracts the year without performing any timezone calculations. This ensures consistent behavior regardless of the user's timezone or the system's default timezone settings. The method directly accesses the year from the ISO 8601 representation of the stored datetime.

The method always returns a number type, never null, undefined, or a string. Even if the GlideDateTime object contains an invalid date, the method will return a number—typically 1970 for epoch-based edge cases. The return value is always a four-digit integer, so years like 2024 return 2024, not 24 or any abbreviated format.

Edge cases occur when working with very early dates (before 1970) or very late dates (after 2038 on some systems). The method handles these gracefully but may return unexpected values for dates outside the typical range. Empty or newly instantiated GlideDateTime objects typically default to the current datetime, so getYearValue() will return the current year rather than throwing an error.

This method pairs logically with getMonthValue(), getDayOfMonthValue(), and other date component extraction methods in the GlideDateTime class. All these methods operate on UTC and return numeric values, making them suitable for mathematical operations and comparisons without string parsing overhead.

When to Use This

Use getYearValue() when you need to extract just the year for mathematical calculations, business logic, or filtering operations. This method excels in scenarios like calculating fiscal years, determining age from birth dates, grouping records by year, or implementing year-based business rules. It's particularly useful when building dynamic GlideRecord queries where you need to filter by year without worrying about timezone complications.

Avoid using this method when you need timezone-aware year extraction for user-facing displays or reports where the user's local timezone matters. In those cases, use getLocalDate() combined with getValue() or format the datetime using getDisplayValue(). Don't use getYearValue() for user interface elements where timezone differences could confuse users about which day or year an event actually occurred in their local time.

Return Value

The method returns a JavaScript number representing the four-digit year in UTC. The value is always an integer between typical year ranges (like 1970 to 2099), though it can handle years outside this range. The return type is specifically a number, not a string, so you can perform direct mathematical operations without type conversion. Values like 2024, 1999, or 2030 are typical return values.

The method never returns null, undefined, or throws an exception under normal circumstances. Invalid or uninitialized GlideDateTime objects may return unexpected years like 1970 (Unix epoch) or the current year, but they still return a valid number. You can safely use the return value in arithmetic operations, comparisons, and string concatenations without null checking, though you should validate that the year makes sense for your business logic context.

Free Newsletter

Enjoying this? Get one deep-dive per week.

Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.

No spam · Unsubscribe anytime

Platform Behavior & Side Effects

  • No database operations are triggered—this is a pure read operation on an existing GlideDateTime object
  • No Business Rules, notifications, or audit entries are created since this only extracts data
  • Performance is very fast—the operation is a simple integer extraction from an in-memory object
  • No caching mechanisms are involved since GlideDateTime objects are already in memory
  • Works identically in before/after Business Rules, Script Includes, and Scheduled Jobs—no behavioral differences
  • User session timezone settings have no impact on the returned value—always uses UTC
  • No ACL checks are performed since this operates on data already loaded in the session