Why Date() function returns different date in Background Script and Client Script?

mihirlimje867
Tera Guru

Hello Community,

I am curious about the Date() function and how it happened.

 

 

When I tried this script in the background script it returned: 4 Jun

 

var today_date = new Date();
gs.info(today_date);

 

 
Result :
Tue Jun 04 2024 22:27:07 GMT-0700 (PDT)

 

When I tried this script in the Client script it returned: 5 Jun

 

 var today_date = new Date();
 alert(today_date);

 

 

 Result :

 

Wed Jun 05 2024 11:00:24 GMT+0530 (India Standard Time)

 

 

 

And again when I click on the "Go to Today" of the date field. it returned :4 Jun 

 

mihirlimje867_0-1717566464493.png

 

And in my Desktop/System current date is : 5 Jun.

 

 

I don't know how it is returned on a different date.

Could you please explain to me?

Thank you.

1 ACCEPTED SOLUTION

Astik Thombare
Tera Sage

Hi @mihirlimje867 ,

 

The reason you're seeing different dates returned by the new Date() function in your ServiceNow scripts is due to time zones.

 

Understanding Time Zones:

  • Date and time are intrinsically linked to a specific location on Earth.
  • Time zones are a way of standardizing time across different regions, accounting for the Earth's rotation.
  • Each time zone has a UTC (Coordinated Universal Time) offset, indicating the difference from UTC.

How new Date() Works in ServiceNow:

  • By default, new Date() in ServiceNow returns the current date and time based on the server's time zone.
  • ServiceNow servers typically have a set time zone configuration.

Your Scenario Breakdown:

  1. Background Script (PDT):

    • If your server is in Pacific Daylight Time (PDT, UTC-7), the script running in the background reflects that time zone (Tue Jun 04 2024 22:27:07 GMT-0700).
  2. Client Script (IST):

    • Client scripts might consider the user's browser time zone. If the user is in India Standard Time (IST, UTC+5.5), the script displays the date according to IST (Wed Jun 05 2024 11:00:24 GMT+0530).
  3. "Go to Today" Date Field:

    • The behavior of the "Go to Today" field might depend on ServiceNow configuration or customization. It's possible it's set to a specific time zone, explaining the difference you're seeing.

Key Points:

  • new Date() reflects the time zone of the environment it's running in (server or user's browser).
  • Time zone discrepancies can cause confusion when dealing with dates and times in ServiceNow.

Recommendations:

  • To ensure consistent date and time handling, consider using ServiceNow's built-in date and time manipulation functions that account for time zones.
  • If necessary, you can explicitly specify the time zone you want to use with the new Date() function by providing relevant parameters (e.g., new Date(2024, 05, 05, 12, 00, 00, UTC)).

By understanding time zones and how new Date() works in ServiceNow, you can effectively manage dates and times in your script

 

If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.

 

      By doing so you help other community members find resolved questions which may relate to an issue they're having

 

 

Thanks,

Astik

View solution in original post

1 REPLY 1

Astik Thombare
Tera Sage

Hi @mihirlimje867 ,

 

The reason you're seeing different dates returned by the new Date() function in your ServiceNow scripts is due to time zones.

 

Understanding Time Zones:

  • Date and time are intrinsically linked to a specific location on Earth.
  • Time zones are a way of standardizing time across different regions, accounting for the Earth's rotation.
  • Each time zone has a UTC (Coordinated Universal Time) offset, indicating the difference from UTC.

How new Date() Works in ServiceNow:

  • By default, new Date() in ServiceNow returns the current date and time based on the server's time zone.
  • ServiceNow servers typically have a set time zone configuration.

Your Scenario Breakdown:

  1. Background Script (PDT):

    • If your server is in Pacific Daylight Time (PDT, UTC-7), the script running in the background reflects that time zone (Tue Jun 04 2024 22:27:07 GMT-0700).
  2. Client Script (IST):

    • Client scripts might consider the user's browser time zone. If the user is in India Standard Time (IST, UTC+5.5), the script displays the date according to IST (Wed Jun 05 2024 11:00:24 GMT+0530).
  3. "Go to Today" Date Field:

    • The behavior of the "Go to Today" field might depend on ServiceNow configuration or customization. It's possible it's set to a specific time zone, explaining the difference you're seeing.

Key Points:

  • new Date() reflects the time zone of the environment it's running in (server or user's browser).
  • Time zone discrepancies can cause confusion when dealing with dates and times in ServiceNow.

Recommendations:

  • To ensure consistent date and time handling, consider using ServiceNow's built-in date and time manipulation functions that account for time zones.
  • If necessary, you can explicitly specify the time zone you want to use with the new Date() function by providing relevant parameters (e.g., new Date(2024, 05, 05, 12, 00, 00, UTC)).

By understanding time zones and how new Date() works in ServiceNow, you can effectively manage dates and times in your script

 

If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.

 

      By doing so you help other community members find resolved questions which may relate to an issue they're having

 

 

Thanks,

Astik