What It Does
The before() method performs a chronological comparison between two GlideDateTime objects. It evaluates whether the datetime represented by the calling object occurs before the datetime represented by the parameter object in time sequence.
Internally, ServiceNow converts both GlideDateTime objects to their UTC millisecond representations and performs a numeric comparison. The platform handles timezone conversions automatically, ensuring that datetimes in different timezones are compared accurately against their actual chronological positions.
The method returns true when the calling datetime is chronologically earlier than the parameter datetime, and false when they are equal or when the calling datetime is later. If either GlideDateTime object contains invalid datetime data, the comparison may return unexpected results rather than throwing an explicit error.
Edge cases include comparing datetimes with identical values (returns false), comparing dates across daylight saving time boundaries, and handling leap seconds. The method treats identical timestamps as neither before nor after each other, making it unsuitable for sorting algorithms that require stable ordering of equal elements.
Related methods in the GlideDateTime class include after() for reverse chronological comparison, equals() for exact datetime matching, and compareTo() for three-way comparison returning -1, 0, or 1.
When to Use This
Use before() when you need to determine if one datetime precedes another in business rules, scheduled jobs, or workflow conditions. Common scenarios include checking if an incident was created before a maintenance window, validating that a task's due date falls before a project deadline, or filtering records based on temporal relationships.
Use compareTo() instead when implementing sorting algorithms or when you need to distinguish between all three comparison states (before, equal, after) in a single operation. Avoid using before() with string datetime values—convert them to GlideDateTime objects first to ensure accurate timezone-aware comparison.
Common misuse patterns include attempting to pass Date objects, datetime strings, or numeric timestamps directly to the method. These will either fail silently or produce incorrect results because the method expects specifically typed GlideDateTime objects that contain timezone and formatting metadata.
Return Value
The method returns a JavaScript boolean value: true if the calling datetime occurs before the parameter datetime, or false in all other cases (equal or after). The return value is never null or undefined under normal circumstances, making it safe to use directly in conditional statements.
When the method encounters invalid input or corrupted datetime data, behavior varies by ServiceNow version and may return false by default or throw a runtime error. Always validate that both GlideDateTime objects contain valid data before comparison to ensure predictable results in production scripts.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.
Platform Behavior & Side Effects
- No Business Rules, ACLs, or notifications fire during datetime comparison—this is purely a computational operation
- Nothing is written to the database; the method performs read-only comparison of in-memory datetime objects
- Performance is fast with minimal overhead—comparable to numeric comparison after initial timezone conversion
- Results are not cached; each method call performs fresh comparison calculation
- Timezone conversions happen automatically using the system's configured timezone settings and user preferences
- Behavior is consistent across Business Rules, Script Includes, and Scheduled Jobs—no contextual differences
- Memory usage is minimal as the method operates on existing GlideDateTime objects without creating new instances